<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta name="generator" content="quarto-1.7.32">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>Data Summaries – modelsummary: Data and Model Summaries in R</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: min(4vw, 1.5em);}
div.column{flex: auto; overflow-x: auto;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
ul.task-list li input[type="checkbox"] {
  width: 0.8em;
  margin: 0 0.8em 0.2em -1em; /* quarto-specific, see https://github.com/quarto-dev/quarto-cli/issues/4556 */ 
  vertical-align: middle;
}
/* CSS for syntax highlighting */
html { -webkit-text-size-adjust: 100%; }
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
  { counter-reset: source-line 0; }
pre.numberSource code > span
  { position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
  { content: counter(source-line);
    position: relative; left: -1em; text-align: right; vertical-align: baseline;
    border: none; display: inline-block;
    -webkit-touch-callout: none; -webkit-user-select: none;
    -khtml-user-select: none; -moz-user-select: none;
    -ms-user-select: none; user-select: none;
    padding: 0 4px; width: 4em;
  }
pre.numberSource { margin-left: 3em;  padding-left: 4px; }
div.sourceCode
  {   }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
</style>

<script src="../site_libs/quarto-nav/quarto-nav.js"></script>
<script src="../site_libs/quarto-nav/headroom.min.js"></script>
<script src="../site_libs/clipboard/clipboard.min.js"></script>
<script src="../site_libs/quarto-search/autocomplete.umd.js"></script>
<script src="../site_libs/quarto-search/fuse.min.js"></script>
<script src="../site_libs/quarto-search/quarto-search.js"></script>
<meta name="quarto:offset" content="../">
<link href="../vignettes/modelsummary.html" rel="next">
<link href="../vignettes/get_started.html" rel="prev">
<script src="../site_libs/quarto-html/quarto.js" type="module"></script>
<script src="../site_libs/quarto-html/tabsets/tabsets.js" type="module"></script>
<script src="../site_libs/quarto-html/popper.min.js"></script>
<script src="../site_libs/quarto-html/tippy.umd.min.js"></script>
<script src="../site_libs/quarto-html/anchor.min.js"></script>
<link href="../site_libs/quarto-html/tippy.css" rel="stylesheet">
<link href="../site_libs/quarto-html/quarto-syntax-highlighting-37eea08aefeeee20ff55810ff984fec1.css" rel="stylesheet" id="quarto-text-highlighting-styles">
<script src="../site_libs/bootstrap/bootstrap.min.js"></script>
<link href="../site_libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="../site_libs/bootstrap/bootstrap-555d7fa6eb067a0c9d334ac7ef1a20f4.min.css" rel="stylesheet" append-hash="true" id="quarto-bootstrap" data-mode="light"><script id="quarto-search-options" type="application/json">{
  "location": "navbar",
  "copy-button": false,
  "collapse-after": 3,
  "panel-placement": "end",
  "type": "overlay",
  "limit": 50,
  "keyboard-shortcut": [
    "f",
    "/",
    "s"
  ],
  "show-item-context": false,
  "language": {
    "search-no-results-text": "No results",
    "search-matching-documents-text": "matching documents",
    "search-copy-link-title": "Copy link to search",
    "search-hide-matches-text": "Hide additional matches",
    "search-more-match-text": "more match in this document",
    "search-more-matches-text": "more matches in this document",
    "search-clear-button-title": "Clear",
    "search-text-placeholder": "",
    "search-detached-cancel-button-title": "Cancel",
    "search-submit-button-title": "Submit",
    "search-label": "Search"
  }
}</script>
</head>
<body class="nav-sidebar floating nav-fixed quarto-light">

<div id="quarto-search-results"></div>
  <header id="quarto-header" class="headroom fixed-top"><nav class="navbar navbar-expand-lg " data-bs-theme="dark"><div class="navbar-container container-fluid">
      <div class="navbar-brand-container mx-auto">
    <a class="navbar-brand" href="../index.html">
    <span class="navbar-title">modelsummary: Data and Model Summaries in R</span>
    </a>
  </div>
            <div id="quarto-search" class="" title="Search"></div>
          <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarCollapse" aria-controls="navbarCollapse" role="menu" aria-expanded="false" aria-label="Toggle navigation" onclick="if (window.quartoToggleHeadroom) { window.quartoToggleHeadroom(); }">
  <span class="navbar-toggler-icon"></span>
</button>
          <div class="collapse navbar-collapse" id="navbarCollapse">
            <ul class="navbar-nav navbar-nav-scroll ms-auto">
<li class="nav-item">
    <a class="nav-link active" href="../vignettes/get_started.html" aria-current="page"> 
<span class="menu-text">Get started</span></a>
  </li>  
  <li class="nav-item compact">
    <a class="nav-link" href="https://github.com/vincentarelbundock/modelsummary/"> <i class="bi bi-github" role="img" aria-label="modelsummary GitHub">
</i> 
<span class="menu-text"></span></a>
  </li>  
</ul>
</div> <!-- /navcollapse -->
            <div class="quarto-navbar-tools">
</div>
      </div> <!-- /container-fluid -->
    </nav><nav class="quarto-secondary-nav"><div class="container-fluid d-flex">
      <button type="button" class="quarto-btn-toggle btn" data-bs-toggle="collapse" role="button" data-bs-target=".quarto-sidebar-collapse-item" aria-controls="quarto-sidebar" aria-expanded="false" aria-label="Toggle sidebar navigation" onclick="if (window.quartoToggleHeadroom) { window.quartoToggleHeadroom(); }">
        <i class="bi bi-layout-text-sidebar-reverse"></i>
      </button>
        <nav class="quarto-page-breadcrumbs" aria-label="breadcrumb"><ol class="breadcrumb"><li class="breadcrumb-item"><a href="../vignettes/datasummary.html">Data Summaries</a></li></ol></nav>
        <a class="flex-grow-1" role="navigation" data-bs-toggle="collapse" data-bs-target=".quarto-sidebar-collapse-item" aria-controls="quarto-sidebar" aria-expanded="false" aria-label="Toggle sidebar navigation" onclick="if (window.quartoToggleHeadroom) { window.quartoToggleHeadroom(); }">      
        </a>
    </div>
  </nav></header><!-- content --><div id="quarto-content" class="quarto-container page-columns page-rows-contents page-layout-article page-navbar">
<!-- sidebar -->
  <nav id="quarto-sidebar" class="sidebar collapse collapse-horizontal quarto-sidebar-collapse-item sidebar-navigation floating overflow-auto"><div class="sidebar-menu-container"> 
    <ul class="list-unstyled mt-1">
<li class="sidebar-item">
  <div class="sidebar-item-container"> 
  <a href="../vignettes/get_started.html" class="sidebar-item-text sidebar-link">
 <span class="menu-text">Getting started</span></a>
  </div>
</li>
        <li class="sidebar-item">
  <div class="sidebar-item-container"> 
  <a href="../vignettes/datasummary.html" class="sidebar-item-text sidebar-link active">
 <span class="menu-text">Data Summaries</span></a>
  </div>
</li>
        <li class="sidebar-item">
  <div class="sidebar-item-container"> 
  <a href="../vignettes/modelsummary.html" class="sidebar-item-text sidebar-link">
 <span class="menu-text">Model Summaries</span></a>
  </div>
</li>
        <li class="sidebar-item">
  <div class="sidebar-item-container"> 
  <a href="../vignettes/modelplot.html" class="sidebar-item-text sidebar-link">
 <span class="menu-text">Model Plots</span></a>
  </div>
</li>
        <li class="sidebar-item">
  <div class="sidebar-item-container"> 
  <a href="../vignettes/modelsummary_extension.html" class="sidebar-item-text sidebar-link">
 <span class="menu-text">Custom</span></a>
  </div>
</li>
        <li class="sidebar-item">
  <div class="sidebar-item-container"> 
  <a href="../vignettes/appearance.html" class="sidebar-item-text sidebar-link">
 <span class="menu-text">Themes and Styles</span></a>
  </div>
</li>
        <li class="sidebar-item sidebar-item-section">
      <div class="sidebar-item-container"> 
            <a class="sidebar-item-text sidebar-link text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-1" role="navigation" aria-expanded="false">
 <span class="menu-text">Functions</span></a>
          <a class="sidebar-item-toggle text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-1" role="navigation" aria-expanded="false" aria-label="Toggle section">
            <i class="bi bi-chevron-right ms-2"></i>
          </a> 
      </div>
      <ul id="quarto-sidebar-section-1" class="collapse list-unstyled sidebar-section depth1 ">
<li class="sidebar-item">
  <div class="sidebar-item-container"> 
  <a href="../man/modelsummary.html" class="sidebar-item-text sidebar-link">
 <span class="menu-text"><code>modelsummary</code></span></a>
  </div>
</li>
          <li class="sidebar-item">
  <div class="sidebar-item-container"> 
  <a href="../man/modelplot.html" class="sidebar-item-text sidebar-link">
 <span class="menu-text"><code>modelplot</code></span></a>
  </div>
</li>
          <li class="sidebar-item">
  <div class="sidebar-item-container"> 
  <a href="../man/datasummary.html" class="sidebar-item-text sidebar-link">
 <span class="menu-text"><code>datasummary</code></span></a>
  </div>
</li>
          <li class="sidebar-item">
  <div class="sidebar-item-container"> 
  <a href="../man/datasummary_skim.html" class="sidebar-item-text sidebar-link">
 <span class="menu-text"><code>datasummary_skim</code></span></a>
  </div>
</li>
          <li class="sidebar-item">
  <div class="sidebar-item-container"> 
  <a href="../man/datasummary_balance.html" class="sidebar-item-text sidebar-link">
 <span class="menu-text"><code>datsummary_balance</code></span></a>
  </div>
</li>
          <li class="sidebar-item">
  <div class="sidebar-item-container"> 
  <a href="../man/datasummary_crosstab.html" class="sidebar-item-text sidebar-link">
 <span class="menu-text"><code>datasummary_crosstab</code></span></a>
  </div>
</li>
          <li class="sidebar-item">
  <div class="sidebar-item-container"> 
  <a href="../man/datasummary_correlation.html" class="sidebar-item-text sidebar-link">
 <span class="menu-text"><code>datasummary_correlation</code></span></a>
  </div>
</li>
          <li class="sidebar-item sidebar-item-section">
      <div class="sidebar-item-container"> 
            <a class="sidebar-item-text sidebar-link text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-2" role="navigation" aria-expanded="false">
 <span class="menu-text">Formatting</span></a>
          <a class="sidebar-item-toggle text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-2" role="navigation" aria-expanded="false" aria-label="Toggle section">
            <i class="bi bi-chevron-right ms-2"></i>
          </a> 
      </div>
      <ul id="quarto-sidebar-section-2" class="collapse list-unstyled sidebar-section depth2 ">
<li class="sidebar-item">
  <div class="sidebar-item-container"> 
  <a href="../man/fmt_decimal.html" class="sidebar-item-text sidebar-link">
 <span class="menu-text"><code>fmt_decimal</code></span></a>
  </div>
</li>
          <li class="sidebar-item">
  <div class="sidebar-item-container"> 
  <a href="../man/fmt_equivalence.html" class="sidebar-item-text sidebar-link">
 <span class="menu-text"><code>fmt_equivalence</code></span></a>
  </div>
</li>
          <li class="sidebar-item">
  <div class="sidebar-item-container"> 
  <a href="../man/fmt_sci.html" class="sidebar-item-text sidebar-link">
 <span class="menu-text"><code>fmt_sci</code></span></a>
  </div>
</li>
          <li class="sidebar-item">
  <div class="sidebar-item-container"> 
  <a href="../man/fmt_significant.html" class="sidebar-item-text sidebar-link">
 <span class="menu-text"><code>fmt_significant</code></span></a>
  </div>
</li>
          <li class="sidebar-item">
  <div class="sidebar-item-container"> 
  <a href="../man/fmt_sprintf.html" class="sidebar-item-text sidebar-link">
 <span class="menu-text"><code>fmt_sprintf</code></span></a>
  </div>
</li>
          <li class="sidebar-item">
  <div class="sidebar-item-container"> 
  <a href="../man/fmt_statistic.html" class="sidebar-item-text sidebar-link">
 <span class="menu-text"><code>fmt_statistic</code></span></a>
  </div>
</li>
          <li class="sidebar-item">
  <div class="sidebar-item-container"> 
  <a href="../man/fmt_term.html" class="sidebar-item-text sidebar-link">
 <span class="menu-text"><code>fmt_term</code></span></a>
  </div>
</li>
      </ul>
</li>
          <li class="sidebar-item sidebar-item-section">
      <div class="sidebar-item-container"> 
            <a class="sidebar-item-text sidebar-link text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-3" role="navigation" aria-expanded="false">
 <span class="menu-text">Utilities</span></a>
          <a class="sidebar-item-toggle text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-3" role="navigation" aria-expanded="false" aria-label="Toggle section">
            <i class="bi bi-chevron-right ms-2"></i>
          </a> 
      </div>
      <ul id="quarto-sidebar-section-3" class="collapse list-unstyled sidebar-section depth2 ">
<li class="sidebar-item">
  <div class="sidebar-item-container"> 
  <a href="../man/get_estimates.html" class="sidebar-item-text sidebar-link">
 <span class="menu-text"><code>get_estimates</code></span></a>
  </div>
</li>
          <li class="sidebar-item">
  <div class="sidebar-item-container"> 
  <a href="../man/get_gof.html" class="sidebar-item-text sidebar-link">
 <span class="menu-text"><code>get_gof</code></span></a>
  </div>
</li>
          <li class="sidebar-item">
  <div class="sidebar-item-container"> 
  <a href="../man/coef_rename.html" class="sidebar-item-text sidebar-link">
 <span class="menu-text"><code>coef_rename</code></span></a>
  </div>
</li>
          <li class="sidebar-item">
  <div class="sidebar-item-container"> 
  <a href="../man/dvnames.html" class="sidebar-item-text sidebar-link">
 <span class="menu-text"><code>dvnames</code></span></a>
  </div>
</li>
          <li class="sidebar-item">
  <div class="sidebar-item-container"> 
  <a href="../man/gof_map.html" class="sidebar-item-text sidebar-link">
 <span class="menu-text"><code>gof_map</code></span></a>
  </div>
</li>
          <li class="sidebar-item">
  <div class="sidebar-item-container"> 
  <a href="../man/datasummary_correlation_format.html" class="sidebar-item-text sidebar-link">
 <span class="menu-text"><code>datasummary_correlation_format</code></span></a>
  </div>
</li>
          <li class="sidebar-item">
  <div class="sidebar-item-container"> 
  <a href="../man/config_modelsummary.html" class="sidebar-item-text sidebar-link">
 <span class="menu-text"><code>config_modelsummary</code></span></a>
  </div>
</li>
          <li class="sidebar-item">
  <div class="sidebar-item-container"> 
  <a href="../man/update_modelsummary.html" class="sidebar-item-text sidebar-link">
 <span class="menu-text"><code>update_modelsummary</code></span></a>
  </div>
</li>
      </ul>
</li>
      </ul>
</li>
        <li class="sidebar-item">
  <div class="sidebar-item-container"> 
  <a href="../NEWS.html" class="sidebar-item-text sidebar-link">
 <span class="menu-text">News</span></a>
  </div>
</li>
        <li class="sidebar-item">
  <div class="sidebar-item-container"> 
  <a href="../LICENSE.html" class="sidebar-item-text sidebar-link">
 <span class="menu-text">License</span></a>
  </div>
</li>
        <li class="sidebar-item">
  <div class="sidebar-item-container"> 
  <a href="../CITATION.html" class="sidebar-item-text sidebar-link">
 <span class="menu-text">Citation</span></a>
  </div>
</li>
    </ul>
</div>
</nav><div id="quarto-sidebar-glass" class="quarto-sidebar-collapse-item" data-bs-toggle="collapse" data-bs-target=".quarto-sidebar-collapse-item"></div>
<!-- margin-sidebar -->
    <div id="quarto-margin-sidebar" class="sidebar margin-sidebar">
        <nav id="TOC" role="doc-toc" class="toc-active"><h2 id="toc-title">On this page</h2>
   
  <ul>
<li><a href="#datasummary_skim" id="toc-datasummary_skim" class="nav-link active" data-scroll-target="#datasummary_skim"><code>datasummary_skim</code></a></li>
  <li><a href="#datasummary_balance" id="toc-datasummary_balance" class="nav-link" data-scroll-target="#datasummary_balance"><code>datasummary_balance</code></a></li>
  <li><a href="#datasummary_correlation" id="toc-datasummary_correlation" class="nav-link" data-scroll-target="#datasummary_correlation"><code>datasummary_correlation</code></a></li>
  <li><a href="#datasummary_crosstab" id="toc-datasummary_crosstab" class="nav-link" data-scroll-target="#datasummary_crosstab"><code>datasummary_crosstab</code></a></li>
  <li>
<a href="#datasummary" id="toc-datasummary" class="nav-link" data-scroll-target="#datasummary"><code>datasummary</code></a>
  <ul class="collapse">
<li><a href="#custom-summary-functions" id="toc-custom-summary-functions" class="nav-link" data-scroll-target="#custom-summary-functions">Custom summary functions</a></li>
  <li><a href="#concatenating-with" id="toc-concatenating-with" class="nav-link" data-scroll-target="#concatenating-with">Concatenating with <code>+</code></a></li>
  <li><a href="#nesting-with" id="toc-nesting-with" class="nav-link" data-scroll-target="#nesting-with">Nesting with <code>*</code></a></li>
  <li><a href="#renaming-with" id="toc-renaming-with" class="nav-link" data-scroll-target="#renaming-with">Renaming with <code>=</code></a></li>
  <li><a href="#counts-and-percentages" id="toc-counts-and-percentages" class="nav-link" data-scroll-target="#counts-and-percentages">Counts and Percentages</a></li>
  <li><a href="#weighted-percentages" id="toc-weighted-percentages" class="nav-link" data-scroll-target="#weighted-percentages">Weighted percentages</a></li>
  <li><a href="#custom-percentages" id="toc-custom-percentages" class="nav-link" data-scroll-target="#custom-percentages">Custom percentages</a></li>
  <li><a href="#factor" id="toc-factor" class="nav-link" data-scroll-target="#factor"><code>Factor</code></a></li>
  <li><a href="#arguments-na.rmtrue" id="toc-arguments-na.rmtrue" class="nav-link" data-scroll-target="#arguments-na.rmtrue"><code>Arguments</code>: <code>na.rm=TRUE</code></a></li>
  <li><a href="#arguments-weighted-mean" id="toc-arguments-weighted-mean" class="nav-link" data-scroll-target="#arguments-weighted-mean"><code>Arguments</code>: Weighted Mean</a></li>
  <li><a href="#logical-subsets" id="toc-logical-subsets" class="nav-link" data-scroll-target="#logical-subsets">Logical subsets</a></li>
  <li>
<a href="#missing-values" id="toc-missing-values" class="nav-link" data-scroll-target="#missing-values">Missing values</a>
  <ul class="collapse">
<li><a href="#functions-and-na.rm" id="toc-functions-and-na.rm" class="nav-link" data-scroll-target="#functions-and-na.rm">Functions and <code>na.rm</code></a></li>
  <li><a href="#empty-crosstab-cells" id="toc-empty-crosstab-cells" class="nav-link" data-scroll-target="#empty-crosstab-cells">Empty crosstab cells</a></li>
  <li><a href="#percent" id="toc-percent" class="nav-link" data-scroll-target="#percent">Percent()</a></li>
  </ul>
</li>
  <li><a href="#add-rows" id="toc-add-rows" class="nav-link" data-scroll-target="#add-rows">Add rows</a></li>
  <li><a href="#add-columns" id="toc-add-columns" class="nav-link" data-scroll-target="#add-columns">Add columns</a></li>
  <li><a href="#rounding" id="toc-rounding" class="nav-link" data-scroll-target="#rounding">Rounding</a></li>
  </ul>
</li>
  <li>
<a href="#titles-and-notes" id="toc-titles-and-notes" class="nav-link" data-scroll-target="#titles-and-notes">Titles and notes</a>
  <ul class="collapse">
<li><a href="#alignment" id="toc-alignment" class="nav-link" data-scroll-target="#alignment">Alignment</a></li>
  <li><a href="#factors" id="toc-factors" class="nav-link" data-scroll-target="#factors">Factors</a></li>
  <li><a href="#output-formats" id="toc-output-formats" class="nav-link" data-scroll-target="#output-formats">Output formats</a></li>
  </ul>
</li>
  </ul></nav>
    </div>
<!-- main -->
<main class="content" id="quarto-document-content"><header id="title-block-header" class="quarto-title-block default"><div class="quarto-title">
<h1 class="title">Data Summaries</h1>
</div>



<div class="quarto-title-meta">

    
  
    
  </div>
  


</header><div class="cell">
<style type="text/css">
.table, th, td {
  font-size: 0.9em;
} 
.table tbody td, .table thead tr {
    white-space: nowrap;
}
</style>
</div>
<p><code>datasummary</code> is a function from the <code>modelsummary</code> package. It allows us to create data summaries, frequency tables, crosstabs, correlation tables, balance tables (aka “Table 1”), and more. It has many benefits:</p>
<ul>
<li>Easy to use.</li>
<li>
<em>Extremely</em> flexible.</li>
<li>Many output formats: HTML, LaTeX, Microsoft Word and Powerpoint, Text/Markdown, PDF, RTF, or Image files.</li>
<li>Embed tables in <code>Rmarkdown</code> or <code>knitr</code> dynamic documents.</li>
<li>
<a href="#customize">Customize the appearance of tables</a> with the <code>tinytable</code>, <code>gt</code>, <code>kableExtra</code> or <code>flextable</code> packages. The possibilities are endless!</li>
</ul>
<p>This tutorial will show how to draw tables like these (and more!):</p>
<center>
<img src="https://user-images.githubusercontent.com/987057/86512021-50839480-bdcc-11ea-893c-8c1e7a277895.png" width="49%"><img src="https://user-images.githubusercontent.com/987057/85771538-045c9400-b6ea-11ea-99af-48363b132f1b.png" width="49%"><img src="https://user-images.githubusercontent.com/987057/85772292-b1cfa780-b6ea-11ea-8ae1-b95c6ddbf0a9.png" width="49%"><img src="https://user-images.githubusercontent.com/987057/85774118-8b127080-b6ec-11ea-913d-d0c13066cf6b.png" width="49%">
</center>
<p><code>datasummary</code> is built around the <em>fantastic</em> <a href="https://cran.r-project.org/web/packages/tables/index.html"><code>tables</code> package for <code>R</code>.</a> It is a thin “wrapper” which adds convenience functions and arguments; a user-interface consistent with <code>modelsummary</code>; cleaner html output; and the ability to export tables to more formats, including <a href="https://gt.rstudio.com"><code>gt</code> tables</a>, <a href="https://davidgohel.github.io/flextable/"><code>flextable</code> objects</a>, and Microsoft Word documents.</p>
<p><code>datasummary</code> is a general-purpose table-making tool. It allows us to build (nearly) any summary table we want by using <a href="#datasummary">simple 2-sided formulae.</a> For example, in the expression <code>x + y ~ mean + sd</code>, the left-hand side of the formula identifies the variables or statistics to display as rows, and the right-hand side defines the columns. Below, we will see how variables and statistics can be “nested” with the <code>*</code> operator to produce tables like the ones above.</p>
<p>In addition to <code>datasummary</code>, the <code>modelsummary</code> package includes a “family” of companion functions named <code>datasummary_*</code>. These functions facilitate the production of standard, commonly used tables. This family currently includes:</p>
<ul>
<li>
<code><a href="https://modelsummary.com/man/datasummary.html">datasummary()</a></code>: Flexible function to create custom tables using 2-sided formulae.</li>
<li>
<code><a href="https://modelsummary.com/man/datasummary_balance.html">datasummary_balance()</a></code>: Group characteristics (e.g., control vs.&nbsp;treatment)</li>
<li>
<code><a href="https://modelsummary.com/man/datasummary_correlation.html">datasummary_correlation()</a></code>: Table of correlations.</li>
<li>
<code><a href="https://modelsummary.com/man/datasummary_skim.html">datasummary_skim()</a></code>: Quick summary of a dataset.</li>
<li>
<code><a href="https://modelsummary.com/man/datasummary_df.html">datasummary_df()</a></code>: Create a table from any dataframe.</li>
<li>
<code><a href="https://modelsummary.com/man/datasummary_crosstab.html">datasummary_crosstab()</a></code>: Cross tabulations of categorical variables.</li>
</ul>
<p>In the next three sections, we illustrate how to use <code>datasummary_balance</code>, <code>datasummary_correlation</code>, <code>datasummary_skim</code>, and <code>datasummary_crosstab</code>. Then, we dive into <code>datasummary</code> itself to highlight its ease and flexibility.</p>
<section id="datasummary_skim" class="level1"><h1><code>datasummary_skim</code></h1>
<p>The first <code>datasummary</code> companion function is called <code>datasummary_skim</code>. It was <em>heavily</em> inspired by one of my favorite data exploration tools for <code>R</code>: the <a href="https://docs.ropensci.org/skimr"><code>skimr</code> package.</a> The goal of this function is to give us a quick look at the data.</p>
<p>To illustrate, we download data from the cool new <a href="https://github.com/allisonhorst/palmerpenguins"><code>palmerpenguins</code> package</a> by Alison Presmanes Hill and Allison Horst. These data were collected at the Palmer Station in Antarctica by Gorman, Williams &amp; Fraser (2014), and they include 3 categorical variables and 4 numeric variables.</p>
<div class="cell">
<div class="sourceCode" id="cb1"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://modelsummary.com">modelsummary</a></span><span class="op">)</span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://tidyverse.tidyverse.org">tidyverse</a></span><span class="op">)</span></span>
<span></span>
<span><span class="va">url</span> <span class="op">&lt;-</span> <span class="st">'https://vincentarelbundock.github.io/Rdatasets/csv/palmerpenguins/penguins.csv'</span></span>
<span><span class="va">penguins</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/utils/read.table.html">read.csv</a></span><span class="op">(</span><span class="va">url</span>, na.strings <span class="op">=</span> <span class="st">""</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>To summarize the numeric variables in the dataset, we type:</p>
<div class="cell">
<div class="sourceCode" id="cb2"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://modelsummary.com/man/datasummary_skim.html">datasummary_skim</a></span><span class="op">(</span><span class="va">penguins</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_mc1p0z9z73e1yxy1y1e4(i, j, css_id) {
          var table = document.getElementById("tinytable_mc1p0z9z73e1yxy1y1e4");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_mc1p0z9z73e1yxy1y1e4(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_mc1p0z9z73e1yxy1y1e4");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '16', j: 0 }, { i: '16', j: 3 }, { i: '16', j: 2 }, { i: '16', j: 1 }, { i: '16', j: 4 }, { i: '16', j: 7 }, { i: '16', j: 6 }, { i: '16', j: 5 }, { i: '16', j: 8 },  ], css_id: 'tinytable_css_68cvkaj6la6z7obqvdco',}, 
          { positions: [ { i: '7', j: 0 }, { i: '7', j: 3 }, { i: '7', j: 2 }, { i: '7', j: 1 }, { i: '7', j: 4 }, { i: '7', j: 7 }, { i: '7', j: 6 }, { i: '7', j: 5 }, { i: '7', j: 8 },  ], css_id: 'tinytable_css_cj2tyqi8cap1n8qrytg7',}, 
          { positions: [ { i: '0', j: 0 }, { i: '0', j: 3 }, { i: '0', j: 2 }, { i: '0', j: 1 }, { i: '0', j: 4 }, { i: '0', j: 7 }, { i: '0', j: 6 }, { i: '0', j: 5 }, { i: '0', j: 8 },  ], css_id: 'tinytable_css_2q6xtjey8okbueyb4kdh',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_mc1p0z9z73e1yxy1y1e4(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_68cvkaj6la6z7obqvdco, .table th.tinytable_css_68cvkaj6la6z7obqvdco { border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_cj2tyqi8cap1n8qrytg7, .table th.tinytable_css_cj2tyqi8cap1n8qrytg7 { border-top: solid black 0.1em; }
      .table td.tinytable_css_2q6xtjey8okbueyb4kdh, .table th.tinytable_css_2q6xtjey8okbueyb4kdh { border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_mc1p0z9z73e1yxy1y1e4" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead><tr>
<th scope="col" data-row="0" data-col="0"> </th>
                <th scope="col" data-row="0" data-col="1">Unique</th>
                <th scope="col" data-row="0" data-col="2">Missing Pct.</th>
                <th scope="col" data-row="0" data-col="3">Mean</th>
                <th scope="col" data-row="0" data-col="4">SD</th>
                <th scope="col" data-row="0" data-col="5">Min</th>
                <th scope="col" data-row="0" data-col="6">Median</th>
                <th scope="col" data-row="0" data-col="7">Max</th>
                <th scope="col" data-row="0" data-col="8">Histogram</th>
              </tr></thead>
<tbody>
<tr>
<td data-row="1" data-col="0">rownames</td>
                  <td data-row="1" data-col="1">344</td>
                  <td data-row="1" data-col="2">0</td>
                  <td data-row="1" data-col="3">172.5</td>
                  <td data-row="1" data-col="4">99.4</td>
                  <td data-row="1" data-col="5">1.0</td>
                  <td data-row="1" data-col="6">172.5</td>
                  <td data-row="1" data-col="7">344.0</td>
                  <td data-row="1" data-col="8"><img src="./tinytable_assets/idx6rtlks1xk53ky3cl9du.png" style="height: 1em;"></td>
                </tr>
<tr>
<td data-row="2" data-col="0">bill_length_mm</td>
                  <td data-row="2" data-col="1">165</td>
                  <td data-row="2" data-col="2">1</td>
                  <td data-row="2" data-col="3">43.9</td>
                  <td data-row="2" data-col="4">5.5</td>
                  <td data-row="2" data-col="5">32.1</td>
                  <td data-row="2" data-col="6">44.5</td>
                  <td data-row="2" data-col="7">59.6</td>
                  <td data-row="2" data-col="8"><img src="./tinytable_assets/idvqdyp8rzbw2wmvrsroak.png" style="height: 1em;"></td>
                </tr>
<tr>
<td data-row="3" data-col="0">bill_depth_mm</td>
                  <td data-row="3" data-col="1">81</td>
                  <td data-row="3" data-col="2">1</td>
                  <td data-row="3" data-col="3">17.2</td>
                  <td data-row="3" data-col="4">2.0</td>
                  <td data-row="3" data-col="5">13.1</td>
                  <td data-row="3" data-col="6">17.3</td>
                  <td data-row="3" data-col="7">21.5</td>
                  <td data-row="3" data-col="8"><img src="./tinytable_assets/id1k0qzl95tkddw063uly4.png" style="height: 1em;"></td>
                </tr>
<tr>
<td data-row="4" data-col="0">flipper_length_mm</td>
                  <td data-row="4" data-col="1">56</td>
                  <td data-row="4" data-col="2">1</td>
                  <td data-row="4" data-col="3">200.9</td>
                  <td data-row="4" data-col="4">14.1</td>
                  <td data-row="4" data-col="5">172.0</td>
                  <td data-row="4" data-col="6">197.0</td>
                  <td data-row="4" data-col="7">231.0</td>
                  <td data-row="4" data-col="8"><img src="./tinytable_assets/idf92z0ginl2fk4kyuspae.png" style="height: 1em;"></td>
                </tr>
<tr>
<td data-row="5" data-col="0">body_mass_g</td>
                  <td data-row="5" data-col="1">95</td>
                  <td data-row="5" data-col="2">1</td>
                  <td data-row="5" data-col="3">4201.8</td>
                  <td data-row="5" data-col="4">802.0</td>
                  <td data-row="5" data-col="5">2700.0</td>
                  <td data-row="5" data-col="6">4050.0</td>
                  <td data-row="5" data-col="7">6300.0</td>
                  <td data-row="5" data-col="8"><img src="./tinytable_assets/idsxffhc4c4gpoetuasdfy.png" style="height: 1em;"></td>
                </tr>
<tr>
<td data-row="6" data-col="0">year</td>
                  <td data-row="6" data-col="1">3</td>
                  <td data-row="6" data-col="2">0</td>
                  <td data-row="6" data-col="3">2008.0</td>
                  <td data-row="6" data-col="4">0.8</td>
                  <td data-row="6" data-col="5">2007.0</td>
                  <td data-row="6" data-col="6">2008.0</td>
                  <td data-row="6" data-col="7">2009.0</td>
                  <td data-row="6" data-col="8"><img src="./tinytable_assets/id90b53km2ne16ub63eg1k.png" style="height: 1em;"></td>
                </tr>
<tr>
<td data-row="7" data-col="0"></td>
                  <td data-row="7" data-col="1"></td>
                  <td data-row="7" data-col="2">N</td>
                  <td data-row="7" data-col="3">%</td>
                  <td data-row="7" data-col="4"> </td>
                  <td data-row="7" data-col="5"> </td>
                  <td data-row="7" data-col="6"> </td>
                  <td data-row="7" data-col="7"> </td>
                  <td data-row="7" data-col="8"> </td>
                </tr>
<tr>
<td data-row="8" data-col="0">species</td>
                  <td data-row="8" data-col="1">Adelie</td>
                  <td data-row="8" data-col="2">152</td>
                  <td data-row="8" data-col="3">44.2</td>
                  <td data-row="8" data-col="4"> </td>
                  <td data-row="8" data-col="5"> </td>
                  <td data-row="8" data-col="6"> </td>
                  <td data-row="8" data-col="7"> </td>
                  <td data-row="8" data-col="8"> </td>
                </tr>
<tr>
<td data-row="9" data-col="0"></td>
                  <td data-row="9" data-col="1">Chinstrap</td>
                  <td data-row="9" data-col="2">68</td>
                  <td data-row="9" data-col="3">19.8</td>
                  <td data-row="9" data-col="4"> </td>
                  <td data-row="9" data-col="5"> </td>
                  <td data-row="9" data-col="6"> </td>
                  <td data-row="9" data-col="7"> </td>
                  <td data-row="9" data-col="8"> </td>
                </tr>
<tr>
<td data-row="10" data-col="0"></td>
                  <td data-row="10" data-col="1">Gentoo</td>
                  <td data-row="10" data-col="2">124</td>
                  <td data-row="10" data-col="3">36.0</td>
                  <td data-row="10" data-col="4"> </td>
                  <td data-row="10" data-col="5"> </td>
                  <td data-row="10" data-col="6"> </td>
                  <td data-row="10" data-col="7"> </td>
                  <td data-row="10" data-col="8"> </td>
                </tr>
<tr>
<td data-row="11" data-col="0">island</td>
                  <td data-row="11" data-col="1">Biscoe</td>
                  <td data-row="11" data-col="2">168</td>
                  <td data-row="11" data-col="3">48.8</td>
                  <td data-row="11" data-col="4"> </td>
                  <td data-row="11" data-col="5"> </td>
                  <td data-row="11" data-col="6"> </td>
                  <td data-row="11" data-col="7"> </td>
                  <td data-row="11" data-col="8"> </td>
                </tr>
<tr>
<td data-row="12" data-col="0"></td>
                  <td data-row="12" data-col="1">Dream</td>
                  <td data-row="12" data-col="2">124</td>
                  <td data-row="12" data-col="3">36.0</td>
                  <td data-row="12" data-col="4"> </td>
                  <td data-row="12" data-col="5"> </td>
                  <td data-row="12" data-col="6"> </td>
                  <td data-row="12" data-col="7"> </td>
                  <td data-row="12" data-col="8"> </td>
                </tr>
<tr>
<td data-row="13" data-col="0"></td>
                  <td data-row="13" data-col="1">Torgersen</td>
                  <td data-row="13" data-col="2">52</td>
                  <td data-row="13" data-col="3">15.1</td>
                  <td data-row="13" data-col="4"> </td>
                  <td data-row="13" data-col="5"> </td>
                  <td data-row="13" data-col="6"> </td>
                  <td data-row="13" data-col="7"> </td>
                  <td data-row="13" data-col="8"> </td>
                </tr>
<tr>
<td data-row="14" data-col="0">sex</td>
                  <td data-row="14" data-col="1">female</td>
                  <td data-row="14" data-col="2">165</td>
                  <td data-row="14" data-col="3">48.0</td>
                  <td data-row="14" data-col="4"> </td>
                  <td data-row="14" data-col="5"> </td>
                  <td data-row="14" data-col="6"> </td>
                  <td data-row="14" data-col="7"> </td>
                  <td data-row="14" data-col="8"> </td>
                </tr>
<tr>
<td data-row="15" data-col="0"></td>
                  <td data-row="15" data-col="1">male</td>
                  <td data-row="15" data-col="2">168</td>
                  <td data-row="15" data-col="3">48.8</td>
                  <td data-row="15" data-col="4"> </td>
                  <td data-row="15" data-col="5"> </td>
                  <td data-row="15" data-col="6"> </td>
                  <td data-row="15" data-col="7"> </td>
                  <td data-row="15" data-col="8"> </td>
                </tr>
<tr>
<td data-row="16" data-col="0"></td>
                  <td data-row="16" data-col="1">NA</td>
                  <td data-row="16" data-col="2">11</td>
                  <td data-row="16" data-col="3">3.2</td>
                  <td data-row="16" data-col="4"> </td>
                  <td data-row="16" data-col="5"> </td>
                  <td data-row="16" data-col="6"> </td>
                  <td data-row="16" data-col="7"> </td>
                  <td data-row="16" data-col="8"> </td>
                </tr>
</tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
<p>To summarize the categorical variables in the dataset, we type:</p>
<div class="cell">
<div class="sourceCode" id="cb3"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://modelsummary.com/man/datasummary_skim.html">datasummary_skim</a></span><span class="op">(</span><span class="va">penguins</span>, type <span class="op">=</span> <span class="st">"categorical"</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_txpdf6x2yznl0qss661n(i, j, css_id) {
          var table = document.getElementById("tinytable_txpdf6x2yznl0qss661n");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_txpdf6x2yznl0qss661n(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_txpdf6x2yznl0qss661n");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '9', j: 2 }, { i: '9', j: 3 },  ], css_id: 'tinytable_css_l02allzc7su991ymu4il',}, 
          { positions: [ { i: '1', j: 2 }, { i: '3', j: 2 }, { i: '4', j: 2 }, { i: '5', j: 2 }, { i: '2', j: 2 }, { i: '7', j: 2 }, { i: '8', j: 2 }, { i: '3', j: 3 }, { i: '6', j: 2 }, { i: '1', j: 3 }, { i: '2', j: 3 }, { i: '7', j: 3 }, { i: '4', j: 3 }, { i: '5', j: 3 }, { i: '6', j: 3 }, { i: '8', j: 3 },  ], css_id: 'tinytable_css_t9tde5pgsxfz9aaf5qa4',}, 
          { positions: [ { i: '0', j: 2 }, { i: '0', j: 3 },  ], css_id: 'tinytable_css_euhfw9v89nvbnot7uro1',}, 
          { positions: [ { i: '9', j: 0 }, { i: '9', j: 1 },  ], css_id: 'tinytable_css_f9rw3by40hx4m1joeiky',}, 
          { positions: [ { i: '1', j: 0 }, { i: '3', j: 0 }, { i: '4', j: 0 }, { i: '5', j: 0 }, { i: '2', j: 0 }, { i: '7', j: 0 }, { i: '8', j: 0 }, { i: '3', j: 1 }, { i: '6', j: 0 }, { i: '1', j: 1 }, { i: '2', j: 1 }, { i: '7', j: 1 }, { i: '4', j: 1 }, { i: '5', j: 1 }, { i: '6', j: 1 }, { i: '8', j: 1 },  ], css_id: 'tinytable_css_e6a7nuy6pmb4vdnm8cjy',}, 
          { positions: [ { i: '0', j: 0 }, { i: '0', j: 1 },  ], css_id: 'tinytable_css_rmdz2h5d2eoa5h6yyg67',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_txpdf6x2yznl0qss661n(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_l02allzc7su991ymu4il, .table th.tinytable_css_l02allzc7su991ymu4il { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_t9tde5pgsxfz9aaf5qa4, .table th.tinytable_css_t9tde5pgsxfz9aaf5qa4 { text-align: right; }
      .table td.tinytable_css_euhfw9v89nvbnot7uro1, .table th.tinytable_css_euhfw9v89nvbnot7uro1 { text-align: right; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_f9rw3by40hx4m1joeiky, .table th.tinytable_css_f9rw3by40hx4m1joeiky { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_e6a7nuy6pmb4vdnm8cjy, .table th.tinytable_css_e6a7nuy6pmb4vdnm8cjy { text-align: left; }
      .table td.tinytable_css_rmdz2h5d2eoa5h6yyg67, .table th.tinytable_css_rmdz2h5d2eoa5h6yyg67 { text-align: left; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_txpdf6x2yznl0qss661n" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead><tr>
<th scope="col" data-row="0" data-col="0"> </th>
                <th scope="col" data-row="0" data-col="1">  </th>
                <th scope="col" data-row="0" data-col="2">N</th>
                <th scope="col" data-row="0" data-col="3">%</th>
              </tr></thead>
<tbody>
<tr>
<td data-row="1" data-col="0">species</td>
                  <td data-row="1" data-col="1">Adelie</td>
                  <td data-row="1" data-col="2">152</td>
                  <td data-row="1" data-col="3">44.2</td>
                </tr>
<tr>
<td data-row="2" data-col="0"></td>
                  <td data-row="2" data-col="1">Chinstrap</td>
                  <td data-row="2" data-col="2">68</td>
                  <td data-row="2" data-col="3">19.8</td>
                </tr>
<tr>
<td data-row="3" data-col="0"></td>
                  <td data-row="3" data-col="1">Gentoo</td>
                  <td data-row="3" data-col="2">124</td>
                  <td data-row="3" data-col="3">36.0</td>
                </tr>
<tr>
<td data-row="4" data-col="0">island</td>
                  <td data-row="4" data-col="1">Biscoe</td>
                  <td data-row="4" data-col="2">168</td>
                  <td data-row="4" data-col="3">48.8</td>
                </tr>
<tr>
<td data-row="5" data-col="0"></td>
                  <td data-row="5" data-col="1">Dream</td>
                  <td data-row="5" data-col="2">124</td>
                  <td data-row="5" data-col="3">36.0</td>
                </tr>
<tr>
<td data-row="6" data-col="0"></td>
                  <td data-row="6" data-col="1">Torgersen</td>
                  <td data-row="6" data-col="2">52</td>
                  <td data-row="6" data-col="3">15.1</td>
                </tr>
<tr>
<td data-row="7" data-col="0">sex</td>
                  <td data-row="7" data-col="1">female</td>
                  <td data-row="7" data-col="2">165</td>
                  <td data-row="7" data-col="3">48.0</td>
                </tr>
<tr>
<td data-row="8" data-col="0"></td>
                  <td data-row="8" data-col="1">male</td>
                  <td data-row="8" data-col="2">168</td>
                  <td data-row="8" data-col="3">48.8</td>
                </tr>
<tr>
<td data-row="9" data-col="0"></td>
                  <td data-row="9" data-col="1">NA</td>
                  <td data-row="9" data-col="2">11</td>
                  <td data-row="9" data-col="3">3.2</td>
                </tr>
</tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
</section><section id="datasummary_balance" class="level1"><h1><code>datasummary_balance</code></h1>
<p>The expressions “balance table” or “Table 1” refer to a type of table which is often printed in the opening pages of a scientific peer-reviewed article. Typically, this table includes basic descriptive statistics about different subsets of the study population. For instance, analysts may want to compare the socio-demographic characteristics of members of the “control” and “treatment” groups in a randomized control trial, or the flipper lengths of male and female penguins. In addition, balance tables often include difference in means tests.</p>
<p>To illustrate how to build a balance table using the <code>datasummary_balance</code> function, we download data about a job training experiment studies in Lalonde (1986). Then, we clean up the data by renaming and recoding a few variables.</p>
<div class="cell">
<div class="sourceCode" id="cb4"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="co"># Download and read data</span></span>
<span><span class="va">training</span> <span class="op">&lt;-</span> <span class="st">'https://vincentarelbundock.github.io/Rdatasets/csv/Ecdat/Treatment.csv'</span></span>
<span><span class="va">training</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/utils/read.table.html">read.csv</a></span><span class="op">(</span><span class="va">training</span>, na.strings <span class="op">=</span> <span class="st">""</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># Rename and recode variables</span></span>
<span><span class="va">training</span> <span class="op">&lt;-</span> <span class="va">training</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html">%&gt;%</a></span></span>
<span>            <span class="fu"><a href="https://dplyr.tidyverse.org/reference/mutate.html">mutate</a></span><span class="op">(</span>`Earnings Before` <span class="op">=</span> <span class="va">re75</span> <span class="op">/</span> <span class="fl">1000</span>,</span>
<span>                   `Earnings After` <span class="op">=</span> <span class="va">re78</span> <span class="op">/</span> <span class="fl">1000</span>,</span>
<span>                   Treatment <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/ifelse.html">ifelse</a></span><span class="op">(</span><span class="va">treat</span> <span class="op">==</span> <span class="cn">TRUE</span>, <span class="st">'Treatment'</span>, <span class="st">'Control'</span><span class="op">)</span>,</span>
<span>                   Married <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/ifelse.html">ifelse</a></span><span class="op">(</span><span class="va">married</span> <span class="op">==</span> <span class="cn">TRUE</span>, <span class="st">'Yes'</span>, <span class="st">'No'</span><span class="op">)</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html">%&gt;%</a></span></span>
<span>            <span class="fu"><a href="https://dplyr.tidyverse.org/reference/select.html">select</a></span><span class="op">(</span><span class="va">`Earnings Before`</span>,</span>
<span>                   <span class="va">`Earnings After`</span>,</span>
<span>                   <span class="va">Treatment</span>,</span>
<span>                   Ethnicity <span class="op">=</span> <span class="va">ethn</span>,</span>
<span>                   Age <span class="op">=</span> <span class="va">age</span>,</span>
<span>                   Education <span class="op">=</span> <span class="va">educ</span>,</span>
<span>                   <span class="va">Married</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>Now, we execute the <code>datasummary_balance</code> function. If the <code>estimatr</code> package is installed, <code>datasummary_balance</code> will calculate the difference in means and test statistics.</p>
<div class="cell">
<div class="sourceCode" id="cb5"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va">caption</span> <span class="op">&lt;-</span> <span class="st">'Descriptive statistics about participants in a job training experiment. The earnings are displayed in 1000s of USD. This table was created using the "datasummary" function from the "modelsummary" package for R.'</span></span>
<span><span class="va">reference</span> <span class="op">&lt;-</span> <span class="st">'Source: Lalonde (1986) American Economic Review.'</span></span>
<span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://modelsummary.com">modelsummary</a></span><span class="op">)</span></span>
<span><span class="fu"><a href="https://modelsummary.com/man/datasummary_balance.html">datasummary_balance</a></span><span class="op">(</span><span class="op">~</span><span class="va">Treatment</span>,</span>
<span>                    data <span class="op">=</span> <span class="va">training</span>,</span>
<span>                    title <span class="op">=</span> <span class="va">caption</span>,</span>
<span>                    notes <span class="op">=</span> <span class="va">reference</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>Note that if the dataset includes columns called “blocks”, “clusters”, or “weights”, this information will automatically be taken into consideration by <code>estimatr</code> when calculating the difference in means and the associated statistics.</p>
<p>Users can also use the <code>~ 1</code> formula to indicate that they want to summarize all the data instead of splitting the analysis across subgroups:</p>
<div class="cell">
<div class="sourceCode" id="cb6"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://modelsummary.com/man/datasummary_balance.html">datasummary_balance</a></span><span class="op">(</span><span class="op">~</span> <span class="fl">1</span>, data <span class="op">=</span> <span class="va">training</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_ww4376pf5k8mfv432gvl(i, j, css_id) {
          var table = document.getElementById("tinytable_ww4376pf5k8mfv432gvl");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_ww4376pf5k8mfv432gvl(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_ww4376pf5k8mfv432gvl");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '12', j: 2 }, { i: '12', j: 3 },  ], css_id: 'tinytable_css_s05mxdve1k9cntbrbnr3',}, 
          { positions: [ { i: '4', j: 2 }, { i: '4', j: 3 },  ], css_id: 'tinytable_css_na9spgqv215cvt6tj1al',}, 
          { positions: [ { i: '2', j: 2 }, { i: '3', j: 2 }, { i: '8', j: 2 }, { i: '1', j: 2 }, { i: '6', j: 2 }, { i: '7', j: 2 }, { i: '3', j: 3 }, { i: '5', j: 2 }, { i: '10', j: 2 }, { i: '11', j: 2 }, { i: '7', j: 3 }, { i: '9', j: 2 }, { i: '1', j: 3 }, { i: '2', j: 3 }, { i: '11', j: 3 }, { i: '8', j: 3 }, { i: '5', j: 3 }, { i: '6', j: 3 }, { i: '9', j: 3 }, { i: '10', j: 3 },  ], css_id: 'tinytable_css_1ae7hjiseixm9mmogx9n',}, 
          { positions: [ { i: '0', j: 2 }, { i: '0', j: 3 },  ], css_id: 'tinytable_css_x5bu5w8mcc9xvfgc5g6p',}, 
          { positions: [ { i: '12', j: 0 }, { i: '12', j: 1 },  ], css_id: 'tinytable_css_jivm11hc00z3ttjh5bne',}, 
          { positions: [ { i: '4', j: 0 }, { i: '4', j: 1 },  ], css_id: 'tinytable_css_188vwl8o32w20rd58saz',}, 
          { positions: [ { i: '2', j: 0 }, { i: '3', j: 0 }, { i: '8', j: 0 }, { i: '1', j: 0 }, { i: '6', j: 0 }, { i: '7', j: 0 }, { i: '3', j: 1 }, { i: '5', j: 0 }, { i: '10', j: 0 }, { i: '11', j: 0 }, { i: '7', j: 1 }, { i: '9', j: 0 }, { i: '1', j: 1 }, { i: '2', j: 1 }, { i: '11', j: 1 }, { i: '8', j: 1 }, { i: '5', j: 1 }, { i: '6', j: 1 }, { i: '9', j: 1 }, { i: '10', j: 1 },  ], css_id: 'tinytable_css_2n6jf3mlwcnbnur758gw',}, 
          { positions: [ { i: '0', j: 0 }, { i: '0', j: 1 },  ], css_id: 'tinytable_css_xk0wyn96ha0jae6h6nj7',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_ww4376pf5k8mfv432gvl(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_s05mxdve1k9cntbrbnr3, .table th.tinytable_css_s05mxdve1k9cntbrbnr3 { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_na9spgqv215cvt6tj1al, .table th.tinytable_css_na9spgqv215cvt6tj1al { text-align: right; border-bottom: solid black 0.05em; }
      .table td.tinytable_css_1ae7hjiseixm9mmogx9n, .table th.tinytable_css_1ae7hjiseixm9mmogx9n { text-align: right; }
      .table td.tinytable_css_x5bu5w8mcc9xvfgc5g6p, .table th.tinytable_css_x5bu5w8mcc9xvfgc5g6p { text-align: right; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_jivm11hc00z3ttjh5bne, .table th.tinytable_css_jivm11hc00z3ttjh5bne { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_188vwl8o32w20rd58saz, .table th.tinytable_css_188vwl8o32w20rd58saz { text-align: left; border-bottom: solid black 0.05em; }
      .table td.tinytable_css_2n6jf3mlwcnbnur758gw, .table th.tinytable_css_2n6jf3mlwcnbnur758gw { text-align: left; }
      .table td.tinytable_css_xk0wyn96ha0jae6h6nj7, .table th.tinytable_css_xk0wyn96ha0jae6h6nj7 { text-align: left; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_ww4376pf5k8mfv432gvl" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead><tr>
<th scope="col" data-row="0" data-col="0"> </th>
                <th scope="col" data-row="0" data-col="1">  </th>
                <th scope="col" data-row="0" data-col="2">Mean</th>
                <th scope="col" data-row="0" data-col="3">Std. Dev.</th>
              </tr></thead>
<tbody>
<tr>
<td data-row="1" data-col="0">Earnings Before</td>
                  <td data-row="1" data-col="1"></td>
                  <td data-row="1" data-col="2">17.9</td>
                  <td data-row="1" data-col="3">13.9</td>
                </tr>
<tr>
<td data-row="2" data-col="0">Earnings After</td>
                  <td data-row="2" data-col="1"></td>
                  <td data-row="2" data-col="2">20.5</td>
                  <td data-row="2" data-col="3">15.6</td>
                </tr>
<tr>
<td data-row="3" data-col="0">Age</td>
                  <td data-row="3" data-col="1"></td>
                  <td data-row="3" data-col="2">34.2</td>
                  <td data-row="3" data-col="3">10.5</td>
                </tr>
<tr>
<td data-row="4" data-col="0">Education</td>
                  <td data-row="4" data-col="1"></td>
                  <td data-row="4" data-col="2">12.0</td>
                  <td data-row="4" data-col="3">3.1</td>
                </tr>
<tr>
<td data-row="5" data-col="0"></td>
                  <td data-row="5" data-col="1"></td>
                  <td data-row="5" data-col="2">N</td>
                  <td data-row="5" data-col="3">Pct.</td>
                </tr>
<tr>
<td data-row="6" data-col="0">Treatment</td>
                  <td data-row="6" data-col="1">Control</td>
                  <td data-row="6" data-col="2">2490</td>
                  <td data-row="6" data-col="3">93.1</td>
                </tr>
<tr>
<td data-row="7" data-col="0"></td>
                  <td data-row="7" data-col="1">Treatment</td>
                  <td data-row="7" data-col="2">185</td>
                  <td data-row="7" data-col="3">6.9</td>
                </tr>
<tr>
<td data-row="8" data-col="0">Ethnicity</td>
                  <td data-row="8" data-col="1">black</td>
                  <td data-row="8" data-col="2">780</td>
                  <td data-row="8" data-col="3">29.2</td>
                </tr>
<tr>
<td data-row="9" data-col="0"></td>
                  <td data-row="9" data-col="1">hispanic</td>
                  <td data-row="9" data-col="2">92</td>
                  <td data-row="9" data-col="3">3.4</td>
                </tr>
<tr>
<td data-row="10" data-col="0"></td>
                  <td data-row="10" data-col="1">other</td>
                  <td data-row="10" data-col="2">1803</td>
                  <td data-row="10" data-col="3">67.4</td>
                </tr>
<tr>
<td data-row="11" data-col="0">Married</td>
                  <td data-row="11" data-col="1">No</td>
                  <td data-row="11" data-col="2">483</td>
                  <td data-row="11" data-col="3">18.1</td>
                </tr>
<tr>
<td data-row="12" data-col="0"></td>
                  <td data-row="12" data-col="1">Yes</td>
                  <td data-row="12" data-col="2">2192</td>
                  <td data-row="12" data-col="3">81.9</td>
                </tr>
</tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
</section><section id="datasummary_correlation" class="level1"><h1><code>datasummary_correlation</code></h1>
<p>The <code>datasummary_correlation</code> accepts a dataframe or tibble, it identifies all the numeric variables, and calculates the correlation between each of those variables:</p>
<div class="cell">
<div class="sourceCode" id="cb7"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://modelsummary.com/man/datasummary_correlation.html">datasummary_correlation</a></span><span class="op">(</span><span class="va">mtcars</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_236w1fmtp745873fm7gb(i, j, css_id) {
          var table = document.getElementById("tinytable_236w1fmtp745873fm7gb");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_236w1fmtp745873fm7gb(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_236w1fmtp745873fm7gb");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '11', j: 10 }, { i: '11', j: 9 }, { i: '11', j: 8 }, { i: '11', j: 7 }, { i: '11', j: 6 }, { i: '11', j: 5 }, { i: '11', j: 4 }, { i: '11', j: 3 }, { i: '11', j: 2 }, { i: '11', j: 1 }, { i: '11', j: 11 },  ], css_id: 'tinytable_css_46fqttwwkzntq1sy6mid',}, 
          { positions: [ { i: '5', j: 1 }, { i: '9', j: 1 }, { i: '1', j: 2 }, { i: '1', j: 1 }, { i: '2', j: 1 }, { i: '3', j: 1 }, { i: '5', j: 2 }, { i: '6', j: 2 }, { i: '6', j: 1 }, { i: '7', j: 1 }, { i: '9', j: 2 }, { i: '10', j: 2 }, { i: '10', j: 1 }, { i: '3', j: 5 }, { i: '1', j: 3 }, { i: '2', j: 3 }, { i: '2', j: 2 }, { i: '3', j: 2 }, { i: '5', j: 3 }, { i: '6', j: 3 }, { i: '7', j: 3 }, { i: '7', j: 2 }, { i: '9', j: 3 }, { i: '10', j: 3 }, { i: '3', j: 7 }, { i: '3', j: 6 }, { i: '1', j: 4 }, { i: '2', j: 4 }, { i: '3', j: 4 }, { i: '3', j: 3 }, { i: '5', j: 4 }, { i: '6', j: 4 }, { i: '7', j: 4 }, { i: '4', j: 1 }, { i: '9', j: 4 }, { i: '10', j: 4 }, { i: '3', j: 8 }, { i: '8', j: 1 }, { i: '1', j: 5 }, { i: '2', j: 5 }, { i: '7', j: 8 }, { i: '4', j: 5 }, { i: '5', j: 5 }, { i: '6', j: 5 }, { i: '7', j: 5 }, { i: '4', j: 2 }, { i: '9', j: 5 }, { i: '10', j: 5 }, { i: '3', j: 9 }, { i: '8', j: 2 }, { i: '1', j: 6 }, { i: '2', j: 6 }, { i: '7', j: 9 }, { i: '4', j: 6 }, { i: '5', j: 6 }, { i: '6', j: 6 }, { i: '7', j: 6 }, { i: '4', j: 3 }, { i: '9', j: 6 }, { i: '10', j: 6 }, { i: '3', j: 10 }, { i: '8', j: 3 }, { i: '1', j: 7 }, { i: '2', j: 7 }, { i: '7', j: 10 }, { i: '4', j: 7 }, { i: '5', j: 7 }, { i: '6', j: 7 }, { i: '7', j: 7 }, { i: '4', j: 4 }, { i: '9', j: 7 }, { i: '10', j: 7 }, { i: '3', j: 11 }, { i: '8', j: 4 }, { i: '1', j: 8 }, { i: '2', j: 8 }, { i: '7', j: 11 }, { i: '4', j: 8 }, { i: '5', j: 8 }, { i: '6', j: 8 }, { i: '8', j: 9 }, { i: '8', j: 8 }, { i: '9', j: 8 }, { i: '10', j: 8 }, { i: '8', j: 6 }, { i: '8', j: 5 }, { i: '1', j: 9 }, { i: '2', j: 9 }, { i: '4', j: 10 }, { i: '4', j: 9 }, { i: '5', j: 9 }, { i: '6', j: 9 }, { i: '8', j: 10 }, { i: '9', j: 10 }, { i: '9', j: 9 }, { i: '10', j: 9 }, { i: '8', j: 7 }, { i: '1', j: 11 }, { i: '1', j: 10 }, { i: '2', j: 10 }, { i: '4', j: 11 }, { i: '5', j: 11 }, { i: '5', j: 10 }, { i: '6', j: 10 }, { i: '8', j: 11 }, { i: '9', j: 11 }, { i: '10', j: 11 }, { i: '10', j: 10 }, { i: '2', j: 11 }, { i: '6', j: 11 },  ], css_id: 'tinytable_css_y0u5tizkbpog66lslcv2',}, 
          { positions: [ { i: '0', j: 1 }, { i: '0', j: 11 }, { i: '0', j: 10 }, { i: '0', j: 9 }, { i: '0', j: 8 }, { i: '0', j: 7 }, { i: '0', j: 6 }, { i: '0', j: 5 }, { i: '0', j: 4 }, { i: '0', j: 3 }, { i: '0', j: 2 },  ], css_id: 'tinytable_css_m81ixfwlbkvh5e5k9lc7',}, 
          { positions: [ { i: '11', j: 0 },  ], css_id: 'tinytable_css_e8f3uuaotmkmsw8xoudl',}, 
          { positions: [ { i: '1', j: 0 }, { i: '2', j: 0 }, { i: '3', j: 0 }, { i: '4', j: 0 }, { i: '5', j: 0 }, { i: '6', j: 0 }, { i: '7', j: 0 }, { i: '8', j: 0 }, { i: '9', j: 0 }, { i: '10', j: 0 },  ], css_id: 'tinytable_css_rtefgeuxocwjzkd1qbvu',}, 
          { positions: [ { i: '0', j: 0 },  ], css_id: 'tinytable_css_fsf666xdd7ay6qz166ct',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_236w1fmtp745873fm7gb(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_46fqttwwkzntq1sy6mid, .table th.tinytable_css_46fqttwwkzntq1sy6mid { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_y0u5tizkbpog66lslcv2, .table th.tinytable_css_y0u5tizkbpog66lslcv2 { text-align: right; }
      .table td.tinytable_css_m81ixfwlbkvh5e5k9lc7, .table th.tinytable_css_m81ixfwlbkvh5e5k9lc7 { text-align: right; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_e8f3uuaotmkmsw8xoudl, .table th.tinytable_css_e8f3uuaotmkmsw8xoudl { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_rtefgeuxocwjzkd1qbvu, .table th.tinytable_css_rtefgeuxocwjzkd1qbvu { text-align: left; }
      .table td.tinytable_css_fsf666xdd7ay6qz166ct, .table th.tinytable_css_fsf666xdd7ay6qz166ct { text-align: left; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_236w1fmtp745873fm7gb" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead><tr>
<th scope="col" data-row="0" data-col="0"> </th>
                <th scope="col" data-row="0" data-col="1">mpg</th>
                <th scope="col" data-row="0" data-col="2">cyl</th>
                <th scope="col" data-row="0" data-col="3">disp</th>
                <th scope="col" data-row="0" data-col="4">hp</th>
                <th scope="col" data-row="0" data-col="5">drat</th>
                <th scope="col" data-row="0" data-col="6">wt</th>
                <th scope="col" data-row="0" data-col="7">qsec</th>
                <th scope="col" data-row="0" data-col="8">vs</th>
                <th scope="col" data-row="0" data-col="9">am</th>
                <th scope="col" data-row="0" data-col="10">gear</th>
                <th scope="col" data-row="0" data-col="11">carb</th>
              </tr></thead>
<tbody>
<tr>
<td data-row="1" data-col="0">mpg</td>
                  <td data-row="1" data-col="1">1</td>
                  <td data-row="1" data-col="2">.</td>
                  <td data-row="1" data-col="3">.</td>
                  <td data-row="1" data-col="4">.</td>
                  <td data-row="1" data-col="5">.</td>
                  <td data-row="1" data-col="6">.</td>
                  <td data-row="1" data-col="7">.</td>
                  <td data-row="1" data-col="8">.</td>
                  <td data-row="1" data-col="9">.</td>
                  <td data-row="1" data-col="10">.</td>
                  <td data-row="1" data-col="11">.</td>
                </tr>
<tr>
<td data-row="2" data-col="0">cyl</td>
                  <td data-row="2" data-col="1">-.85</td>
                  <td data-row="2" data-col="2">1</td>
                  <td data-row="2" data-col="3">.</td>
                  <td data-row="2" data-col="4">.</td>
                  <td data-row="2" data-col="5">.</td>
                  <td data-row="2" data-col="6">.</td>
                  <td data-row="2" data-col="7">.</td>
                  <td data-row="2" data-col="8">.</td>
                  <td data-row="2" data-col="9">.</td>
                  <td data-row="2" data-col="10">.</td>
                  <td data-row="2" data-col="11">.</td>
                </tr>
<tr>
<td data-row="3" data-col="0">disp</td>
                  <td data-row="3" data-col="1">-.85</td>
                  <td data-row="3" data-col="2">.90</td>
                  <td data-row="3" data-col="3">1</td>
                  <td data-row="3" data-col="4">.</td>
                  <td data-row="3" data-col="5">.</td>
                  <td data-row="3" data-col="6">.</td>
                  <td data-row="3" data-col="7">.</td>
                  <td data-row="3" data-col="8">.</td>
                  <td data-row="3" data-col="9">.</td>
                  <td data-row="3" data-col="10">.</td>
                  <td data-row="3" data-col="11">.</td>
                </tr>
<tr>
<td data-row="4" data-col="0">hp</td>
                  <td data-row="4" data-col="1">-.78</td>
                  <td data-row="4" data-col="2">.83</td>
                  <td data-row="4" data-col="3">.79</td>
                  <td data-row="4" data-col="4">1</td>
                  <td data-row="4" data-col="5">.</td>
                  <td data-row="4" data-col="6">.</td>
                  <td data-row="4" data-col="7">.</td>
                  <td data-row="4" data-col="8">.</td>
                  <td data-row="4" data-col="9">.</td>
                  <td data-row="4" data-col="10">.</td>
                  <td data-row="4" data-col="11">.</td>
                </tr>
<tr>
<td data-row="5" data-col="0">drat</td>
                  <td data-row="5" data-col="1">.68</td>
                  <td data-row="5" data-col="2">-.70</td>
                  <td data-row="5" data-col="3">-.71</td>
                  <td data-row="5" data-col="4">-.45</td>
                  <td data-row="5" data-col="5">1</td>
                  <td data-row="5" data-col="6">.</td>
                  <td data-row="5" data-col="7">.</td>
                  <td data-row="5" data-col="8">.</td>
                  <td data-row="5" data-col="9">.</td>
                  <td data-row="5" data-col="10">.</td>
                  <td data-row="5" data-col="11">.</td>
                </tr>
<tr>
<td data-row="6" data-col="0">wt</td>
                  <td data-row="6" data-col="1">-.87</td>
                  <td data-row="6" data-col="2">.78</td>
                  <td data-row="6" data-col="3">.89</td>
                  <td data-row="6" data-col="4">.66</td>
                  <td data-row="6" data-col="5">-.71</td>
                  <td data-row="6" data-col="6">1</td>
                  <td data-row="6" data-col="7">.</td>
                  <td data-row="6" data-col="8">.</td>
                  <td data-row="6" data-col="9">.</td>
                  <td data-row="6" data-col="10">.</td>
                  <td data-row="6" data-col="11">.</td>
                </tr>
<tr>
<td data-row="7" data-col="0">qsec</td>
                  <td data-row="7" data-col="1">.42</td>
                  <td data-row="7" data-col="2">-.59</td>
                  <td data-row="7" data-col="3">-.43</td>
                  <td data-row="7" data-col="4">-.71</td>
                  <td data-row="7" data-col="5">.09</td>
                  <td data-row="7" data-col="6">-.17</td>
                  <td data-row="7" data-col="7">1</td>
                  <td data-row="7" data-col="8">.</td>
                  <td data-row="7" data-col="9">.</td>
                  <td data-row="7" data-col="10">.</td>
                  <td data-row="7" data-col="11">.</td>
                </tr>
<tr>
<td data-row="8" data-col="0">vs</td>
                  <td data-row="8" data-col="1">.66</td>
                  <td data-row="8" data-col="2">-.81</td>
                  <td data-row="8" data-col="3">-.71</td>
                  <td data-row="8" data-col="4">-.72</td>
                  <td data-row="8" data-col="5">.44</td>
                  <td data-row="8" data-col="6">-.55</td>
                  <td data-row="8" data-col="7">.74</td>
                  <td data-row="8" data-col="8">1</td>
                  <td data-row="8" data-col="9">.</td>
                  <td data-row="8" data-col="10">.</td>
                  <td data-row="8" data-col="11">.</td>
                </tr>
<tr>
<td data-row="9" data-col="0">am</td>
                  <td data-row="9" data-col="1">.60</td>
                  <td data-row="9" data-col="2">-.52</td>
                  <td data-row="9" data-col="3">-.59</td>
                  <td data-row="9" data-col="4">-.24</td>
                  <td data-row="9" data-col="5">.71</td>
                  <td data-row="9" data-col="6">-.69</td>
                  <td data-row="9" data-col="7">-.23</td>
                  <td data-row="9" data-col="8">.17</td>
                  <td data-row="9" data-col="9">1</td>
                  <td data-row="9" data-col="10">.</td>
                  <td data-row="9" data-col="11">.</td>
                </tr>
<tr>
<td data-row="10" data-col="0">gear</td>
                  <td data-row="10" data-col="1">.48</td>
                  <td data-row="10" data-col="2">-.49</td>
                  <td data-row="10" data-col="3">-.56</td>
                  <td data-row="10" data-col="4">-.13</td>
                  <td data-row="10" data-col="5">.70</td>
                  <td data-row="10" data-col="6">-.58</td>
                  <td data-row="10" data-col="7">-.21</td>
                  <td data-row="10" data-col="8">.21</td>
                  <td data-row="10" data-col="9">.79</td>
                  <td data-row="10" data-col="10">1</td>
                  <td data-row="10" data-col="11">.</td>
                </tr>
<tr>
<td data-row="11" data-col="0">carb</td>
                  <td data-row="11" data-col="1">-.55</td>
                  <td data-row="11" data-col="2">.53</td>
                  <td data-row="11" data-col="3">.39</td>
                  <td data-row="11" data-col="4">.75</td>
                  <td data-row="11" data-col="5">-.09</td>
                  <td data-row="11" data-col="6">.43</td>
                  <td data-row="11" data-col="7">-.66</td>
                  <td data-row="11" data-col="8">-.57</td>
                  <td data-row="11" data-col="9">.06</td>
                  <td data-row="11" data-col="10">.27</td>
                  <td data-row="11" data-col="11">1</td>
                </tr>
</tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
<p>The values displayed in this table are equivalent to those obtained by calling: <code>cor(x, use='pairwise.complete.obs')</code>.</p>
<p>The <code>datasummary_correlation</code> function has a <code>methods</code> argument. The default value is <code>"pearson"</code>, but it also accepts other values like <code>"spearman"</code>. In addition, <code>method</code> can accept any function which takes a data frame and returns a matrix. For example, we can create a custom function to display information from the <code>correlation</code> package. This allows us to include significance stars even if the <code>stars</code> argument is not supported by default in <code><a href="https://modelsummary.com/man/datasummary_correlation.html">datasummary_correlation()</a></code>:</p>
<div class="cell">
<div class="sourceCode" id="cb8"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://easystats.github.io/correlation/">correlation</a></span><span class="op">)</span></span>
<span></span>
<span><span class="va">fun</span> <span class="op">&lt;-</span> <span class="kw">function</span><span class="op">(</span><span class="va">x</span><span class="op">)</span> <span class="op">{</span></span>
<span>  <span class="va">out</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://easystats.github.io/correlation/reference/correlation.html">correlation</a></span><span class="op">(</span><span class="va">x</span><span class="op">)</span> <span class="op">|&gt;</span></span>
<span>    <span class="fu"><a href="https://rdrr.io/r/base/summary.html">summary</a></span><span class="op">(</span><span class="op">)</span> <span class="op">|&gt;</span></span>
<span>    <span class="fu"><a href="https://rdrr.io/r/base/format.html">format</a></span><span class="op">(</span><span class="fl">2</span><span class="op">)</span> <span class="op">|&gt;</span> </span>
<span>    <span class="fu"><a href="https://rdrr.io/r/base/matrix.html">as.matrix</a></span><span class="op">(</span><span class="op">)</span></span>
<span>  <span class="fu"><a href="https://rdrr.io/r/base/row.names.html">row.names</a></span><span class="op">(</span><span class="va">out</span><span class="op">)</span> <span class="op">&lt;-</span> <span class="va">out</span><span class="op">[</span>, <span class="fl">1</span><span class="op">]</span></span>
<span>  <span class="va">out</span> <span class="op">&lt;-</span> <span class="va">out</span><span class="op">[</span>, <span class="fl">2</span><span class="op">:</span><span class="fu"><a href="https://rdrr.io/r/base/nrow.html">ncol</a></span><span class="op">(</span><span class="va">out</span><span class="op">)</span><span class="op">]</span></span>
<span>  <span class="kw"><a href="https://rdrr.io/r/base/function.html">return</a></span><span class="op">(</span><span class="va">out</span><span class="op">)</span></span>
<span><span class="op">}</span></span>
<span></span>
<span><span class="fu"><a href="https://modelsummary.com/man/datasummary_correlation.html">datasummary_correlation</a></span><span class="op">(</span><span class="va">mtcars</span>, method <span class="op">=</span> <span class="va">fun</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_re5bqyizada8t6j6ejnc(i, j, css_id) {
          var table = document.getElementById("tinytable_re5bqyizada8t6j6ejnc");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_re5bqyizada8t6j6ejnc(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_re5bqyizada8t6j6ejnc");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '10', j: 3 }, { i: '10', j: 2 }, { i: '10', j: 9 }, { i: '10', j: 1 }, { i: '10', j: 4 }, { i: '10', j: 6 }, { i: '10', j: 7 }, { i: '10', j: 5 }, { i: '10', j: 8 }, { i: '10', j: 10 },  ], css_id: 'tinytable_css_1k7ovtitlei6cp9ll17z',}, 
          { positions: [ { i: '1', j: 1 }, { i: '6', j: 5 }, { i: '3', j: 1 }, { i: '5', j: 1 }, { i: '3', j: 2 }, { i: '2', j: 1 }, { i: '1', j: 2 }, { i: '6', j: 2 }, { i: '7', j: 2 }, { i: '6', j: 1 }, { i: '5', j: 2 }, { i: '6', j: 6 }, { i: '9', j: 1 }, { i: '1', j: 3 }, { i: '7', j: 1 }, { i: '3', j: 3 }, { i: '2', j: 2 }, { i: '5', j: 3 }, { i: '6', j: 3 }, { i: '7', j: 3 }, { i: '8', j: 3 }, { i: '9', j: 3 }, { i: '6', j: 7 }, { i: '9', j: 2 }, { i: '1', j: 4 }, { i: '2', j: 4 }, { i: '3', j: 4 }, { i: '2', j: 3 }, { i: '5', j: 4 }, { i: '6', j: 4 }, { i: '7', j: 4 }, { i: '8', j: 4 }, { i: '9', j: 4 }, { i: '6', j: 8 }, { i: '4', j: 1 }, { i: '1', j: 5 }, { i: '2', j: 5 }, { i: '3', j: 5 }, { i: '8', j: 1 }, { i: '5', j: 5 }, { i: '2', j: 9 }, { i: '7', j: 5 }, { i: '8', j: 5 }, { i: '9', j: 5 }, { i: '6', j: 9 }, { i: '4', j: 2 }, { i: '1', j: 6 }, { i: '2', j: 6 }, { i: '3', j: 6 }, { i: '8', j: 2 }, { i: '5', j: 6 }, { i: '2', j: 10 }, { i: '7', j: 6 }, { i: '8', j: 6 }, { i: '9', j: 6 }, { i: '6', j: 10 }, { i: '4', j: 3 }, { i: '1', j: 7 }, { i: '2', j: 7 }, { i: '3', j: 7 }, { i: '4', j: 7 }, { i: '5', j: 7 }, { i: '8', j: 8 }, { i: '7', j: 7 }, { i: '8', j: 7 }, { i: '9', j: 7 }, { i: '1', j: 9 }, { i: '4', j: 4 }, { i: '1', j: 8 }, { i: '2', j: 8 }, { i: '3', j: 8 }, { i: '4', j: 8 }, { i: '5', j: 8 }, { i: '8', j: 9 }, { i: '7', j: 8 }, { i: '3', j: 10 }, { i: '9', j: 8 }, { i: '1', j: 10 }, { i: '4', j: 5 }, { i: '5', j: 9 }, { i: '4', j: 10 }, { i: '3', j: 9 }, { i: '4', j: 9 }, { i: '9', j: 9 }, { i: '8', j: 10 }, { i: '7', j: 9 }, { i: '5', j: 10 }, { i: '7', j: 10 }, { i: '4', j: 6 }, { i: '9', j: 10 },  ], css_id: 'tinytable_css_cwqtolms8dzsng9n4z7k',}, 
          { positions: [ { i: '0', j: 1 }, { i: '0', j: 3 }, { i: '0', j: 6 }, { i: '0', j: 4 }, { i: '0', j: 5 }, { i: '0', j: 7 }, { i: '0', j: 10 }, { i: '0', j: 2 }, { i: '0', j: 9 }, { i: '0', j: 8 },  ], css_id: 'tinytable_css_m5u8u5b2h43ck4rbrltj',}, 
          { positions: [ { i: '10', j: 0 },  ], css_id: 'tinytable_css_uu4ssk8gjw9uf3zrjvew',}, 
          { positions: [ { i: '1', j: 0 }, { i: '2', j: 0 }, { i: '3', j: 0 }, { i: '4', j: 0 }, { i: '5', j: 0 }, { i: '6', j: 0 }, { i: '7', j: 0 }, { i: '8', j: 0 }, { i: '9', j: 0 },  ], css_id: 'tinytable_css_wgzr2yssnsdctzcoachw',}, 
          { positions: [ { i: '0', j: 0 },  ], css_id: 'tinytable_css_ko7k4ywt3j04qt2z4tj3',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_re5bqyizada8t6j6ejnc(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_1k7ovtitlei6cp9ll17z, .table th.tinytable_css_1k7ovtitlei6cp9ll17z { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_cwqtolms8dzsng9n4z7k, .table th.tinytable_css_cwqtolms8dzsng9n4z7k { text-align: right; }
      .table td.tinytable_css_m5u8u5b2h43ck4rbrltj, .table th.tinytable_css_m5u8u5b2h43ck4rbrltj { text-align: right; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_uu4ssk8gjw9uf3zrjvew, .table th.tinytable_css_uu4ssk8gjw9uf3zrjvew { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_wgzr2yssnsdctzcoachw, .table th.tinytable_css_wgzr2yssnsdctzcoachw { text-align: left; }
      .table td.tinytable_css_ko7k4ywt3j04qt2z4tj3, .table th.tinytable_css_ko7k4ywt3j04qt2z4tj3 { text-align: left; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_re5bqyizada8t6j6ejnc" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead><tr>
<th scope="col" data-row="0" data-col="0"> </th>
                <th scope="col" data-row="0" data-col="1">carb</th>
                <th scope="col" data-row="0" data-col="2">gear</th>
                <th scope="col" data-row="0" data-col="3">am</th>
                <th scope="col" data-row="0" data-col="4">vs</th>
                <th scope="col" data-row="0" data-col="5">qsec</th>
                <th scope="col" data-row="0" data-col="6">wt</th>
                <th scope="col" data-row="0" data-col="7">drat</th>
                <th scope="col" data-row="0" data-col="8">hp</th>
                <th scope="col" data-row="0" data-col="9">disp</th>
                <th scope="col" data-row="0" data-col="10">cyl</th>
              </tr></thead>
<tbody>
<tr>
<td data-row="1" data-col="0">mpg</td>
                  <td data-row="1" data-col="1">-.55*</td>
                  <td data-row="1" data-col="2">.48</td>
                  <td data-row="1" data-col="3">.60**</td>
                  <td data-row="1" data-col="4">.66**</td>
                  <td data-row="1" data-col="5">.42</td>
                  <td data-row="1" data-col="6">-.87***</td>
                  <td data-row="1" data-col="7">.68***</td>
                  <td data-row="1" data-col="8">-.78***</td>
                  <td data-row="1" data-col="9">-.85***</td>
                  <td data-row="1" data-col="10">-.85***</td>
                </tr>
<tr>
<td data-row="2" data-col="0">cyl</td>
                  <td data-row="2" data-col="1">.53*</td>
                  <td data-row="2" data-col="2">-.49</td>
                  <td data-row="2" data-col="3">-.52*</td>
                  <td data-row="2" data-col="4">-.81***</td>
                  <td data-row="2" data-col="5">-.59*</td>
                  <td data-row="2" data-col="6">.78***</td>
                  <td data-row="2" data-col="7">-.70***</td>
                  <td data-row="2" data-col="8">.83***</td>
                  <td data-row="2" data-col="9">.90***</td>
                  <td data-row="2" data-col="10"></td>
                </tr>
<tr>
<td data-row="3" data-col="0">disp</td>
                  <td data-row="3" data-col="1">.39</td>
                  <td data-row="3" data-col="2">-.56*</td>
                  <td data-row="3" data-col="3">-.59*</td>
                  <td data-row="3" data-col="4">-.71***</td>
                  <td data-row="3" data-col="5">-.43</td>
                  <td data-row="3" data-col="6">.89***</td>
                  <td data-row="3" data-col="7">-.71***</td>
                  <td data-row="3" data-col="8">.79***</td>
                  <td data-row="3" data-col="9"></td>
                  <td data-row="3" data-col="10"></td>
                </tr>
<tr>
<td data-row="4" data-col="0">hp</td>
                  <td data-row="4" data-col="1">.75***</td>
                  <td data-row="4" data-col="2">-.13</td>
                  <td data-row="4" data-col="3">-.24</td>
                  <td data-row="4" data-col="4">-.72***</td>
                  <td data-row="4" data-col="5">-.71***</td>
                  <td data-row="4" data-col="6">.66**</td>
                  <td data-row="4" data-col="7">-.45</td>
                  <td data-row="4" data-col="8"></td>
                  <td data-row="4" data-col="9"></td>
                  <td data-row="4" data-col="10"></td>
                </tr>
<tr>
<td data-row="5" data-col="0">drat</td>
                  <td data-row="5" data-col="1">-.09</td>
                  <td data-row="5" data-col="2">.70***</td>
                  <td data-row="5" data-col="3">.71***</td>
                  <td data-row="5" data-col="4">.44</td>
                  <td data-row="5" data-col="5">.09</td>
                  <td data-row="5" data-col="6">-.71***</td>
                  <td data-row="5" data-col="7"></td>
                  <td data-row="5" data-col="8"></td>
                  <td data-row="5" data-col="9"></td>
                  <td data-row="5" data-col="10"></td>
                </tr>
<tr>
<td data-row="6" data-col="0">wt</td>
                  <td data-row="6" data-col="1">.43</td>
                  <td data-row="6" data-col="2">-.58*</td>
                  <td data-row="6" data-col="3">-.69***</td>
                  <td data-row="6" data-col="4">-.55*</td>
                  <td data-row="6" data-col="5">-.17</td>
                  <td data-row="6" data-col="6"></td>
                  <td data-row="6" data-col="7"></td>
                  <td data-row="6" data-col="8"></td>
                  <td data-row="6" data-col="9"></td>
                  <td data-row="6" data-col="10"></td>
                </tr>
<tr>
<td data-row="7" data-col="0">qsec</td>
                  <td data-row="7" data-col="1">-.66**</td>
                  <td data-row="7" data-col="2">-.21</td>
                  <td data-row="7" data-col="3">-.23</td>
                  <td data-row="7" data-col="4">.74***</td>
                  <td data-row="7" data-col="5"></td>
                  <td data-row="7" data-col="6"></td>
                  <td data-row="7" data-col="7"></td>
                  <td data-row="7" data-col="8"></td>
                  <td data-row="7" data-col="9"></td>
                  <td data-row="7" data-col="10"></td>
                </tr>
<tr>
<td data-row="8" data-col="0">vs</td>
                  <td data-row="8" data-col="1">-.57*</td>
                  <td data-row="8" data-col="2">.21</td>
                  <td data-row="8" data-col="3">.17</td>
                  <td data-row="8" data-col="4"></td>
                  <td data-row="8" data-col="5"></td>
                  <td data-row="8" data-col="6"></td>
                  <td data-row="8" data-col="7"></td>
                  <td data-row="8" data-col="8"></td>
                  <td data-row="8" data-col="9"></td>
                  <td data-row="8" data-col="10"></td>
                </tr>
<tr>
<td data-row="9" data-col="0">am</td>
                  <td data-row="9" data-col="1">.06</td>
                  <td data-row="9" data-col="2">.79***</td>
                  <td data-row="9" data-col="3"></td>
                  <td data-row="9" data-col="4"></td>
                  <td data-row="9" data-col="5"></td>
                  <td data-row="9" data-col="6"></td>
                  <td data-row="9" data-col="7"></td>
                  <td data-row="9" data-col="8"></td>
                  <td data-row="9" data-col="9"></td>
                  <td data-row="9" data-col="10"></td>
                </tr>
<tr>
<td data-row="10" data-col="0">gear</td>
                  <td data-row="10" data-col="1">.27</td>
                  <td data-row="10" data-col="2"></td>
                  <td data-row="10" data-col="3"></td>
                  <td data-row="10" data-col="4"></td>
                  <td data-row="10" data-col="5"></td>
                  <td data-row="10" data-col="6"></td>
                  <td data-row="10" data-col="7"></td>
                  <td data-row="10" data-col="8"></td>
                  <td data-row="10" data-col="9"></td>
                  <td data-row="10" data-col="10"></td>
                </tr>
</tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
</section><section id="datasummary_crosstab" class="level1"><h1><code>datasummary_crosstab</code></h1>
<p>A cross tabulation is often useful to explore the association between two categorical variables.</p>
<div class="cell">
<div class="sourceCode" id="cb9"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://modelsummary.com">modelsummary</a></span><span class="op">)</span></span>
<span><span class="va">url</span> <span class="op">&lt;-</span> <span class="st">'https://vincentarelbundock.github.io/Rdatasets/csv/palmerpenguins/penguins.csv'</span></span>
<span><span class="va">penguins</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/utils/read.table.html">read.csv</a></span><span class="op">(</span><span class="va">url</span>, na.strings <span class="op">=</span> <span class="st">""</span><span class="op">)</span></span>
<span></span>
<span><span class="fu"><a href="https://modelsummary.com/man/datasummary_crosstab.html">datasummary_crosstab</a></span><span class="op">(</span><span class="va">species</span> <span class="op">~</span> <span class="va">sex</span>, data <span class="op">=</span> <span class="va">penguins</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_fl4omujnuvs4i1poe5j8(i, j, css_id) {
          var table = document.getElementById("tinytable_fl4omujnuvs4i1poe5j8");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_fl4omujnuvs4i1poe5j8(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_fl4omujnuvs4i1poe5j8");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '8', j: 3 }, { i: '8', j: 2 }, { i: '8', j: 4 },  ], css_id: 'tinytable_css_jc1a9lmwds8iqrpz5ly0',}, 
          { positions: [ { i: '2', j: 2 }, { i: '3', j: 2 }, { i: '4', j: 2 }, { i: '6', j: 2 }, { i: '7', j: 2 }, { i: '3', j: 4 }, { i: '1', j: 2 }, { i: '1', j: 3 }, { i: '2', j: 3 }, { i: '3', j: 3 }, { i: '4', j: 3 }, { i: '5', j: 3 }, { i: '6', j: 3 }, { i: '7', j: 3 }, { i: '7', j: 4 }, { i: '5', j: 2 }, { i: '1', j: 4 }, { i: '2', j: 4 }, { i: '4', j: 4 }, { i: '5', j: 4 }, { i: '6', j: 4 },  ], css_id: 'tinytable_css_cbs0wdlkdn5xerdbdc7z',}, 
          { positions: [ { i: '0', j: 2 }, { i: '0', j: 4 }, { i: '0', j: 3 },  ], css_id: 'tinytable_css_bk13b80bwg9n0goki2on',}, 
          { positions: [ { i: '8', j: 0 }, { i: '8', j: 1 },  ], css_id: 'tinytable_css_jc6pwzpk3scln00k0jrq',}, 
          { positions: [ { i: '2', j: 0 }, { i: '3', j: 0 }, { i: '4', j: 0 }, { i: '1', j: 0 }, { i: '6', j: 0 }, { i: '7', j: 0 }, { i: '3', j: 1 }, { i: '5', j: 0 }, { i: '1', j: 1 }, { i: '2', j: 1 }, { i: '7', j: 1 }, { i: '4', j: 1 }, { i: '5', j: 1 }, { i: '6', j: 1 },  ], css_id: 'tinytable_css_ymbuoyjkpmtfbksd4q43',}, 
          { positions: [ { i: '0', j: 0 }, { i: '0', j: 1 },  ], css_id: 'tinytable_css_wc6lse1a9bl4ybvzcxgu',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_fl4omujnuvs4i1poe5j8(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_jc1a9lmwds8iqrpz5ly0, .table th.tinytable_css_jc1a9lmwds8iqrpz5ly0 { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_cbs0wdlkdn5xerdbdc7z, .table th.tinytable_css_cbs0wdlkdn5xerdbdc7z { text-align: right; }
      .table td.tinytable_css_bk13b80bwg9n0goki2on, .table th.tinytable_css_bk13b80bwg9n0goki2on { text-align: right; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_jc6pwzpk3scln00k0jrq, .table th.tinytable_css_jc6pwzpk3scln00k0jrq { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_ymbuoyjkpmtfbksd4q43, .table th.tinytable_css_ymbuoyjkpmtfbksd4q43 { text-align: left; }
      .table td.tinytable_css_wc6lse1a9bl4ybvzcxgu, .table th.tinytable_css_wc6lse1a9bl4ybvzcxgu { text-align: left; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_fl4omujnuvs4i1poe5j8" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead><tr>
<th scope="col" data-row="0" data-col="0">species</th>
                <th scope="col" data-row="0" data-col="1"> </th>
                <th scope="col" data-row="0" data-col="2">female</th>
                <th scope="col" data-row="0" data-col="3">male</th>
                <th scope="col" data-row="0" data-col="4">All</th>
              </tr></thead>
<tbody>
<tr>
<td data-row="1" data-col="0">Adelie</td>
                  <td data-row="1" data-col="1">N</td>
                  <td data-row="1" data-col="2">73</td>
                  <td data-row="1" data-col="3">73</td>
                  <td data-row="1" data-col="4">152</td>
                </tr>
<tr>
<td data-row="2" data-col="0"></td>
                  <td data-row="2" data-col="1">% row</td>
                  <td data-row="2" data-col="2">48.0</td>
                  <td data-row="2" data-col="3">48.0</td>
                  <td data-row="2" data-col="4">100.0</td>
                </tr>
<tr>
<td data-row="3" data-col="0">Chinstrap</td>
                  <td data-row="3" data-col="1">N</td>
                  <td data-row="3" data-col="2">34</td>
                  <td data-row="3" data-col="3">34</td>
                  <td data-row="3" data-col="4">68</td>
                </tr>
<tr>
<td data-row="4" data-col="0"></td>
                  <td data-row="4" data-col="1">% row</td>
                  <td data-row="4" data-col="2">50.0</td>
                  <td data-row="4" data-col="3">50.0</td>
                  <td data-row="4" data-col="4">100.0</td>
                </tr>
<tr>
<td data-row="5" data-col="0">Gentoo</td>
                  <td data-row="5" data-col="1">N</td>
                  <td data-row="5" data-col="2">58</td>
                  <td data-row="5" data-col="3">61</td>
                  <td data-row="5" data-col="4">124</td>
                </tr>
<tr>
<td data-row="6" data-col="0"></td>
                  <td data-row="6" data-col="1">% row</td>
                  <td data-row="6" data-col="2">46.8</td>
                  <td data-row="6" data-col="3">49.2</td>
                  <td data-row="6" data-col="4">100.0</td>
                </tr>
<tr>
<td data-row="7" data-col="0">All</td>
                  <td data-row="7" data-col="1">N</td>
                  <td data-row="7" data-col="2">165</td>
                  <td data-row="7" data-col="3">168</td>
                  <td data-row="7" data-col="4">344</td>
                </tr>
<tr>
<td data-row="8" data-col="0"></td>
                  <td data-row="8" data-col="1">% row</td>
                  <td data-row="8" data-col="2">48.0</td>
                  <td data-row="8" data-col="3">48.8</td>
                  <td data-row="8" data-col="4">100.0</td>
                </tr>
</tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
<p>You can create multi-level crosstabs by specifying interactions using the <code>*</code> operator:</p>
<div class="cell">
<div class="sourceCode" id="cb10"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://modelsummary.com/man/datasummary_crosstab.html">datasummary_crosstab</a></span><span class="op">(</span><span class="va">species</span> <span class="op">~</span> <span class="va">sex</span> <span class="op">*</span> <span class="va">island</span>, data <span class="op">=</span> <span class="va">penguins</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_1ks9pw6r6jcpxoi61tyf(i, j, css_id) {
          var table = document.getElementById("tinytable_1ks9pw6r6jcpxoi61tyf");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_1ks9pw6r6jcpxoi61tyf(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_1ks9pw6r6jcpxoi61tyf");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '-1', j: 4 }, { i: '-1', j: 5 }, { i: '-1', j: 6 }, { i: '-1', j: 7 }, { i: '-1', j: 8 },  ], css_id: 'tinytable_css_s8s24mjyq2gik8khu6x3',}, 
          { positions: [ { i: '8', j: 2 }, { i: '8', j: 5 }, { i: '8', j: 4 }, { i: '8', j: 8 }, { i: '8', j: 3 }, { i: '8', j: 7 }, { i: '8', j: 6 },  ], css_id: 'tinytable_css_n7gw3br6i26ja4xc6nra',}, 
          { positions: [ { i: '4', j: 2 }, { i: '1', j: 4 }, { i: '1', j: 2 }, { i: '2', j: 2 }, { i: '2', j: 3 }, { i: '5', j: 2 }, { i: '4', j: 3 }, { i: '1', j: 3 }, { i: '3', j: 2 }, { i: '7', j: 3 }, { i: '1', j: 5 }, { i: '5', j: 3 }, { i: '7', j: 2 }, { i: '4', j: 5 }, { i: '2', j: 4 }, { i: '6', j: 2 }, { i: '4', j: 4 }, { i: '5', j: 4 }, { i: '3', j: 3 }, { i: '7', j: 4 }, { i: '1', j: 6 }, { i: '6', j: 3 }, { i: '3', j: 6 }, { i: '4', j: 6 }, { i: '2', j: 5 }, { i: '3', j: 5 }, { i: '7', j: 6 }, { i: '5', j: 5 }, { i: '3', j: 4 }, { i: '7', j: 5 }, { i: '1', j: 7 }, { i: '6', j: 4 }, { i: '3', j: 7 }, { i: '4', j: 7 }, { i: '2', j: 6 }, { i: '6', j: 7 }, { i: '7', j: 7 }, { i: '5', j: 6 }, { i: '6', j: 6 }, { i: '4', j: 8 }, { i: '1', j: 8 }, { i: '6', j: 5 }, { i: '3', j: 8 }, { i: '5', j: 7 }, { i: '2', j: 7 }, { i: '6', j: 8 }, { i: '7', j: 8 }, { i: '5', j: 8 }, { i: '2', j: 8 },  ], css_id: 'tinytable_css_cpgv96bdbdzmpixegob5',}, 
          { positions: [ { i: '-1', j: 2 }, { i: '-1', j: 3 },  ], css_id: 'tinytable_css_7ry9w5ofupyozfqys400',}, 
          { positions: [ { i: '0', j: 2 }, { i: '0', j: 6 }, { i: '0', j: 3 }, { i: '0', j: 4 }, { i: '0', j: 5 }, { i: '0', j: 7 }, { i: '0', j: 8 },  ], css_id: 'tinytable_css_1wct0p92dco2mfndnwdt',}, 
          { positions: [ { i: '8', j: 0 }, { i: '8', j: 1 },  ], css_id: 'tinytable_css_vjn8hxh3c9by5vrbwh85',}, 
          { positions: [ { i: '3', j: 0 }, { i: '4', j: 0 }, { i: '1', j: 0 }, { i: '2', j: 0 }, { i: '7', j: 0 }, { i: '2', j: 1 }, { i: '5', j: 0 }, { i: '6', j: 0 }, { i: '1', j: 1 }, { i: '6', j: 1 }, { i: '3', j: 1 }, { i: '4', j: 1 }, { i: '5', j: 1 }, { i: '7', j: 1 },  ], css_id: 'tinytable_css_uylx14foenmdg2e6yktk',}, 
          { positions: [ { i: '-1', j: 0 }, { i: '-1', j: 1 },  ], css_id: 'tinytable_css_a9qnf1e4k04ighfx15ow',}, 
          { positions: [ { i: '0', j: 0 }, { i: '0', j: 1 },  ], css_id: 'tinytable_css_50rgnjlnw1qfns9bv5wa',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_1ks9pw6r6jcpxoi61tyf(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_s8s24mjyq2gik8khu6x3, .table th.tinytable_css_s8s24mjyq2gik8khu6x3 { text-align: right; text-align: center; border-top: solid #d3d8dc 0.1em; text-align: center; }
      .table td.tinytable_css_n7gw3br6i26ja4xc6nra, .table th.tinytable_css_n7gw3br6i26ja4xc6nra { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_cpgv96bdbdzmpixegob5, .table th.tinytable_css_cpgv96bdbdzmpixegob5 { text-align: right; }
      .table td.tinytable_css_7ry9w5ofupyozfqys400, .table th.tinytable_css_7ry9w5ofupyozfqys400 { text-align: right; text-align: center; border-top: solid #d3d8dc 0.1em; text-align: center; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_1wct0p92dco2mfndnwdt, .table th.tinytable_css_1wct0p92dco2mfndnwdt { text-align: right; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_vjn8hxh3c9by5vrbwh85, .table th.tinytable_css_vjn8hxh3c9by5vrbwh85 { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_uylx14foenmdg2e6yktk, .table th.tinytable_css_uylx14foenmdg2e6yktk { text-align: left; }
      .table td.tinytable_css_a9qnf1e4k04ighfx15ow, .table th.tinytable_css_a9qnf1e4k04ighfx15ow { text-align: left; text-align: center; border-top: solid #d3d8dc 0.1em; text-align: center; }
      .table td.tinytable_css_50rgnjlnw1qfns9bv5wa, .table th.tinytable_css_50rgnjlnw1qfns9bv5wa { text-align: left; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_1ks9pw6r6jcpxoi61tyf" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead>
<tr>
<th scope="col" align="center" colspan="1" data-row="-1" data-col="0"> </th>
<th scope="col" align="center" colspan="1" data-row="-1" data-col="1"> </th>
<th scope="col" align="center" colspan="3" data-row="-1" data-col="2">female</th>
<th scope="col" align="center" colspan="3" data-row="-1" data-col="3">male</th>
<th scope="col" align="center" colspan="1" data-row="-1" data-col="4"> </th>
</tr>
<tr>
<th scope="col" data-row="0" data-col="0">species</th>
                <th scope="col" data-row="0" data-col="1"> </th>
                <th scope="col" data-row="0" data-col="2">Biscoe</th>
                <th scope="col" data-row="0" data-col="3">Dream</th>
                <th scope="col" data-row="0" data-col="4">Torgersen</th>
                <th scope="col" data-row="0" data-col="5">Biscoe</th>
                <th scope="col" data-row="0" data-col="6">Dream</th>
                <th scope="col" data-row="0" data-col="7">Torgersen</th>
                <th scope="col" data-row="0" data-col="8">All</th>
              </tr>
</thead>
<tbody>
<tr>
<td data-row="1" data-col="0">Adelie</td>
                  <td data-row="1" data-col="1">N</td>
                  <td data-row="1" data-col="2">22</td>
                  <td data-row="1" data-col="3">27</td>
                  <td data-row="1" data-col="4">24</td>
                  <td data-row="1" data-col="5">22</td>
                  <td data-row="1" data-col="6">28</td>
                  <td data-row="1" data-col="7">23</td>
                  <td data-row="1" data-col="8">152</td>
                </tr>
<tr>
<td data-row="2" data-col="0"></td>
                  <td data-row="2" data-col="1">% row</td>
                  <td data-row="2" data-col="2">14.5</td>
                  <td data-row="2" data-col="3">17.8</td>
                  <td data-row="2" data-col="4">15.8</td>
                  <td data-row="2" data-col="5">14.5</td>
                  <td data-row="2" data-col="6">18.4</td>
                  <td data-row="2" data-col="7">15.1</td>
                  <td data-row="2" data-col="8">100.0</td>
                </tr>
<tr>
<td data-row="3" data-col="0">Chinstrap</td>
                  <td data-row="3" data-col="1">N</td>
                  <td data-row="3" data-col="2">0</td>
                  <td data-row="3" data-col="3">34</td>
                  <td data-row="3" data-col="4">0</td>
                  <td data-row="3" data-col="5">0</td>
                  <td data-row="3" data-col="6">34</td>
                  <td data-row="3" data-col="7">0</td>
                  <td data-row="3" data-col="8">68</td>
                </tr>
<tr>
<td data-row="4" data-col="0"></td>
                  <td data-row="4" data-col="1">% row</td>
                  <td data-row="4" data-col="2">0.0</td>
                  <td data-row="4" data-col="3">50.0</td>
                  <td data-row="4" data-col="4">0.0</td>
                  <td data-row="4" data-col="5">0.0</td>
                  <td data-row="4" data-col="6">50.0</td>
                  <td data-row="4" data-col="7">0.0</td>
                  <td data-row="4" data-col="8">100.0</td>
                </tr>
<tr>
<td data-row="5" data-col="0">Gentoo</td>
                  <td data-row="5" data-col="1">N</td>
                  <td data-row="5" data-col="2">58</td>
                  <td data-row="5" data-col="3">0</td>
                  <td data-row="5" data-col="4">0</td>
                  <td data-row="5" data-col="5">61</td>
                  <td data-row="5" data-col="6">0</td>
                  <td data-row="5" data-col="7">0</td>
                  <td data-row="5" data-col="8">124</td>
                </tr>
<tr>
<td data-row="6" data-col="0"></td>
                  <td data-row="6" data-col="1">% row</td>
                  <td data-row="6" data-col="2">46.8</td>
                  <td data-row="6" data-col="3">0.0</td>
                  <td data-row="6" data-col="4">0.0</td>
                  <td data-row="6" data-col="5">49.2</td>
                  <td data-row="6" data-col="6">0.0</td>
                  <td data-row="6" data-col="7">0.0</td>
                  <td data-row="6" data-col="8">100.0</td>
                </tr>
<tr>
<td data-row="7" data-col="0">All</td>
                  <td data-row="7" data-col="1">N</td>
                  <td data-row="7" data-col="2">80</td>
                  <td data-row="7" data-col="3">61</td>
                  <td data-row="7" data-col="4">24</td>
                  <td data-row="7" data-col="5">83</td>
                  <td data-row="7" data-col="6">62</td>
                  <td data-row="7" data-col="7">23</td>
                  <td data-row="7" data-col="8">344</td>
                </tr>
<tr>
<td data-row="8" data-col="0"></td>
                  <td data-row="8" data-col="1">% row</td>
                  <td data-row="8" data-col="2">23.3</td>
                  <td data-row="8" data-col="3">17.7</td>
                  <td data-row="8" data-col="4">7.0</td>
                  <td data-row="8" data-col="5">24.1</td>
                  <td data-row="8" data-col="6">18.0</td>
                  <td data-row="8" data-col="7">6.7</td>
                  <td data-row="8" data-col="8">100.0</td>
                </tr>
</tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
<p>By default, the cell counts and row percentages are shown for each cell, and both row and column totals are calculated. To show cell percentages or column percentages, or to drop row and column totals, adjust the <code>statistic</code> argument. This argument accepts a formula that follows the <code>datasummary</code> “language”. To understand exactly how it works, you may find it useful to skip to the <code>datasummary</code> tutorial in the next section. Example:</p>
<div class="cell">
<div class="sourceCode" id="cb11"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://modelsummary.com/man/datasummary_crosstab.html">datasummary_crosstab</a></span><span class="op">(</span><span class="va">species</span> <span class="op">~</span> <span class="va">sex</span>,</span>
<span>                     statistic <span class="op">=</span> <span class="fl">1</span> <span class="op">~</span> <span class="fu"><a href="https://dmurdoch.github.io/tables/reference/Percent.html">Percent</a></span><span class="op">(</span><span class="st">"col"</span><span class="op">)</span>,</span>
<span>                     data <span class="op">=</span> <span class="va">penguins</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_dl1cgyuxeljzgf4linju(i, j, css_id) {
          var table = document.getElementById("tinytable_dl1cgyuxeljzgf4linju");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_dl1cgyuxeljzgf4linju(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_dl1cgyuxeljzgf4linju");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '4', j: 2 }, { i: '4', j: 3 },  ], css_id: 'tinytable_css_az5m9xvveb7c25qyl321',}, 
          { positions: [ { i: '2', j: 2 }, { i: '3', j: 2 }, { i: '3', j: 3 }, { i: '1', j: 2 }, { i: '1', j: 3 }, { i: '2', j: 3 },  ], css_id: 'tinytable_css_srzpiin97weirq7odz4h',}, 
          { positions: [ { i: '0', j: 2 }, { i: '0', j: 3 },  ], css_id: 'tinytable_css_thjohirvtalb97o4y7hm',}, 
          { positions: [ { i: '4', j: 0 }, { i: '4', j: 1 },  ], css_id: 'tinytable_css_68twmikm68qy4pk6h30r',}, 
          { positions: [ { i: '2', j: 0 }, { i: '3', j: 0 }, { i: '3', j: 1 }, { i: '1', j: 0 }, { i: '1', j: 1 }, { i: '2', j: 1 },  ], css_id: 'tinytable_css_xt27cuq41uy878lc3h32',}, 
          { positions: [ { i: '0', j: 0 }, { i: '0', j: 1 },  ], css_id: 'tinytable_css_wlrwsawa3o9nrjd9wr3e',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_dl1cgyuxeljzgf4linju(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_az5m9xvveb7c25qyl321, .table th.tinytable_css_az5m9xvveb7c25qyl321 { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_srzpiin97weirq7odz4h, .table th.tinytable_css_srzpiin97weirq7odz4h { text-align: right; }
      .table td.tinytable_css_thjohirvtalb97o4y7hm, .table th.tinytable_css_thjohirvtalb97o4y7hm { text-align: right; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_68twmikm68qy4pk6h30r, .table th.tinytable_css_68twmikm68qy4pk6h30r { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_xt27cuq41uy878lc3h32, .table th.tinytable_css_xt27cuq41uy878lc3h32 { text-align: left; }
      .table td.tinytable_css_wlrwsawa3o9nrjd9wr3e, .table th.tinytable_css_wlrwsawa3o9nrjd9wr3e { text-align: left; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_dl1cgyuxeljzgf4linju" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead><tr>
<th scope="col" data-row="0" data-col="0">species</th>
                <th scope="col" data-row="0" data-col="1"> </th>
                <th scope="col" data-row="0" data-col="2">female</th>
                <th scope="col" data-row="0" data-col="3">male</th>
              </tr></thead>
<tbody>
<tr>
<td data-row="1" data-col="0">Adelie</td>
                  <td data-row="1" data-col="1">% col</td>
                  <td data-row="1" data-col="2">44.2</td>
                  <td data-row="1" data-col="3">43.5</td>
                </tr>
<tr>
<td data-row="2" data-col="0">Chinstrap</td>
                  <td data-row="2" data-col="1">% col</td>
                  <td data-row="2" data-col="2">20.6</td>
                  <td data-row="2" data-col="3">20.2</td>
                </tr>
<tr>
<td data-row="3" data-col="0">Gentoo</td>
                  <td data-row="3" data-col="1">% col</td>
                  <td data-row="3" data-col="2">35.2</td>
                  <td data-row="3" data-col="3">36.3</td>
                </tr>
<tr>
<td data-row="4" data-col="0">All</td>
                  <td data-row="4" data-col="1">% col</td>
                  <td data-row="4" data-col="2">100.0</td>
                  <td data-row="4" data-col="3">100.0</td>
                </tr>
</tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
<p>See <code><a href="https://modelsummary.com/man/datasummary_crosstab.html">?datasummary_crosstab</a></code> for more details.</p>
</section><section id="datasummary" class="level1"><h1><code>datasummary</code></h1>
<p><code>datasummary</code> tables are specified using a 2-sided formula, divided by a tilde <code>~</code>. The left-hand side describes the rows; the right-hand side describes the columns. To illustrate how this works, we will again be using the <a href="https://allisonhorst.github.io/palmerpenguins/"><code>palmerpenguins dataset</code></a>:</p>
<p>To display the <code>flipper_length_mm</code> variable as a row and the <code>mean</code> as a column, we type:</p>
<div class="cell">
<div class="sourceCode" id="cb12"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">flipper_length_mm</span> <span class="op">~</span> <span class="va">Mean</span>,</span>
<span>            data <span class="op">=</span> <span class="va">penguins</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_iyc4qernr4t7ruf3eaeg(i, j, css_id) {
          var table = document.getElementById("tinytable_iyc4qernr4t7ruf3eaeg");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_iyc4qernr4t7ruf3eaeg(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_iyc4qernr4t7ruf3eaeg");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '1', j: 1 },  ], css_id: 'tinytable_css_fyv2m1265eefwi4leki1',}, 
          { positions: [ { i: '0', j: 1 },  ], css_id: 'tinytable_css_32v616synbdm655h8oo3',}, 
          { positions: [ { i: '1', j: 0 },  ], css_id: 'tinytable_css_eejsjf9spbln74cdrioz',}, 
          { positions: [ { i: '0', j: 0 },  ], css_id: 'tinytable_css_bg26of4rxogmkle7tvt9',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_iyc4qernr4t7ruf3eaeg(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_fyv2m1265eefwi4leki1, .table th.tinytable_css_fyv2m1265eefwi4leki1 { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_32v616synbdm655h8oo3, .table th.tinytable_css_32v616synbdm655h8oo3 { text-align: right; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_eejsjf9spbln74cdrioz, .table th.tinytable_css_eejsjf9spbln74cdrioz { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_bg26of4rxogmkle7tvt9, .table th.tinytable_css_bg26of4rxogmkle7tvt9 { text-align: left; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_iyc4qernr4t7ruf3eaeg" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead><tr>
<th scope="col" data-row="0" data-col="0"> </th>
                <th scope="col" data-row="0" data-col="1">Mean</th>
              </tr></thead>
<tbody><tr>
<td data-row="1" data-col="0">flipper_length_mm</td>
                  <td data-row="1" data-col="1">200.92</td>
                </tr></tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
<p>To flip rows and columns, we flip the left and right-hand sides of the formula:</p>
<div class="cell">
<div class="sourceCode" id="cb13"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">Mean</span> <span class="op">~</span> <span class="va">flipper_length_mm</span>,</span>
<span>            data <span class="op">=</span> <span class="va">penguins</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_6v09r6thu1rruh4b63mq(i, j, css_id) {
          var table = document.getElementById("tinytable_6v09r6thu1rruh4b63mq");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_6v09r6thu1rruh4b63mq(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_6v09r6thu1rruh4b63mq");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '1', j: 1 },  ], css_id: 'tinytable_css_pgdplvbjhcpnho8qttye',}, 
          { positions: [ { i: '0', j: 1 },  ], css_id: 'tinytable_css_l8rry1vqd559r0dmrrui',}, 
          { positions: [ { i: '1', j: 0 },  ], css_id: 'tinytable_css_2fkrjymtbz9e4j256mgh',}, 
          { positions: [ { i: '0', j: 0 },  ], css_id: 'tinytable_css_aei4ydao2fvfp18yovg1',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_6v09r6thu1rruh4b63mq(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_pgdplvbjhcpnho8qttye, .table th.tinytable_css_pgdplvbjhcpnho8qttye { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_l8rry1vqd559r0dmrrui, .table th.tinytable_css_l8rry1vqd559r0dmrrui { text-align: right; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_2fkrjymtbz9e4j256mgh, .table th.tinytable_css_2fkrjymtbz9e4j256mgh { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_aei4ydao2fvfp18yovg1, .table th.tinytable_css_aei4ydao2fvfp18yovg1 { text-align: left; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_6v09r6thu1rruh4b63mq" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead><tr>
<th scope="col" data-row="0" data-col="0"> </th>
                <th scope="col" data-row="0" data-col="1">flipper_length_mm</th>
              </tr></thead>
<tbody><tr>
<td data-row="1" data-col="0">Mean</td>
                  <td data-row="1" data-col="1">200.92</td>
                </tr></tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
<section id="custom-summary-functions" class="level2"><h2 class="anchored" data-anchor-id="custom-summary-functions">Custom summary functions</h2>
<p>The <code>Mean</code> function is a shortcut supplied by <code>modelsummary</code>, and it is equivalent to <code>mean(x,na.rm=TRUE)</code>. Since the <code>flipper_length_mm</code> variable includes missing observation, using the <code>mean</code> formula (with default <code>na.rm=FALSE</code>) would produce a missing/empty cell:</p>
<div class="cell">
<div class="sourceCode" id="cb14"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">flipper_length_mm</span> <span class="op">~</span> <span class="va">mean</span>,</span>
<span>            data <span class="op">=</span> <span class="va">penguins</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_l7cba5fr7apj1krnywu9(i, j, css_id) {
          var table = document.getElementById("tinytable_l7cba5fr7apj1krnywu9");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_l7cba5fr7apj1krnywu9(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_l7cba5fr7apj1krnywu9");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '1', j: 1 },  ], css_id: 'tinytable_css_otsq1rnyfx3dtorhqvww',}, 
          { positions: [ { i: '0', j: 1 },  ], css_id: 'tinytable_css_hlvyviafnjzyh2grm4d5',}, 
          { positions: [ { i: '1', j: 0 },  ], css_id: 'tinytable_css_5hhbq92b1lcr5td8j5wm',}, 
          { positions: [ { i: '0', j: 0 },  ], css_id: 'tinytable_css_plabjzo3isv8ab1mvjg5',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_l7cba5fr7apj1krnywu9(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_otsq1rnyfx3dtorhqvww, .table th.tinytable_css_otsq1rnyfx3dtorhqvww { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_hlvyviafnjzyh2grm4d5, .table th.tinytable_css_hlvyviafnjzyh2grm4d5 { text-align: right; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_5hhbq92b1lcr5td8j5wm, .table th.tinytable_css_5hhbq92b1lcr5td8j5wm { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_plabjzo3isv8ab1mvjg5, .table th.tinytable_css_plabjzo3isv8ab1mvjg5 { text-align: left; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_l7cba5fr7apj1krnywu9" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead><tr>
<th scope="col" data-row="0" data-col="0"> </th>
                <th scope="col" data-row="0" data-col="1">mean</th>
              </tr></thead>
<tbody><tr>
<td data-row="1" data-col="0">flipper_length_mm</td>
                  <td data-row="1" data-col="1"></td>
                </tr></tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
<p><code>modelsummary</code> supplies these functions: <code>Mean</code>, <code>SD</code>, <code>Min</code>, <code>Max</code>, <code>Median</code>, <code>P0</code>, <code>P25</code>, <code>P50</code>, <code>P75</code>, <code>P100</code>, <code>Histogram</code>, and a few more (see the package documentation).</p>
<p>Users are also free to create and use their own custom summaries. Any <code>R</code> function which takes a vector and produces a single value is acceptable. For example, the <code>Range</code> functions return a numerical value, and the <code>MinMax</code> returns a string:</p>
<div class="cell">
<div class="sourceCode" id="cb15"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va">Range</span> <span class="op">&lt;-</span> <span class="kw">function</span><span class="op">(</span><span class="va">x</span><span class="op">)</span> <span class="fu"><a href="https://rdrr.io/r/base/Extremes.html">max</a></span><span class="op">(</span><span class="va">x</span>, na.rm <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span> <span class="op">-</span> <span class="fu"><a href="https://rdrr.io/r/base/Extremes.html">min</a></span><span class="op">(</span><span class="va">x</span>, na.rm <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span></span>
<span></span>
<span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">flipper_length_mm</span> <span class="op">~</span> <span class="va">Range</span>,</span>
<span>            data <span class="op">=</span> <span class="va">penguins</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_oxc3e43pacstj4zm7g6b(i, j, css_id) {
          var table = document.getElementById("tinytable_oxc3e43pacstj4zm7g6b");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_oxc3e43pacstj4zm7g6b(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_oxc3e43pacstj4zm7g6b");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '1', j: 1 },  ], css_id: 'tinytable_css_abwb59q3gfkfvsk1gaat',}, 
          { positions: [ { i: '0', j: 1 },  ], css_id: 'tinytable_css_ozk2x5enwl21nsbvbzx2',}, 
          { positions: [ { i: '1', j: 0 },  ], css_id: 'tinytable_css_7z8v3ct7uc3i27tsd7q1',}, 
          { positions: [ { i: '0', j: 0 },  ], css_id: 'tinytable_css_burxjbipscljnkimkve3',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_oxc3e43pacstj4zm7g6b(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_abwb59q3gfkfvsk1gaat, .table th.tinytable_css_abwb59q3gfkfvsk1gaat { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_ozk2x5enwl21nsbvbzx2, .table th.tinytable_css_ozk2x5enwl21nsbvbzx2 { text-align: right; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_7z8v3ct7uc3i27tsd7q1, .table th.tinytable_css_7z8v3ct7uc3i27tsd7q1 { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_burxjbipscljnkimkve3, .table th.tinytable_css_burxjbipscljnkimkve3 { text-align: left; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_oxc3e43pacstj4zm7g6b" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead><tr>
<th scope="col" data-row="0" data-col="0"> </th>
                <th scope="col" data-row="0" data-col="1">Range</th>
              </tr></thead>
<tbody><tr>
<td data-row="1" data-col="0">flipper_length_mm</td>
                  <td data-row="1" data-col="1">59.00</td>
                </tr></tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
<div class="sourceCode" id="cb16"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va">MinMax</span> <span class="op">&lt;-</span> <span class="kw">function</span><span class="op">(</span><span class="va">x</span><span class="op">)</span> <span class="fu"><a href="https://rdrr.io/r/base/paste.html">paste0</a></span><span class="op">(</span><span class="st">'['</span>, <span class="fu"><a href="https://rdrr.io/r/base/Extremes.html">min</a></span><span class="op">(</span><span class="va">x</span>, na.rm <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span>, <span class="st">', '</span>, <span class="fu"><a href="https://rdrr.io/r/base/Extremes.html">max</a></span><span class="op">(</span><span class="va">x</span>, na.rm <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span>, <span class="st">']'</span><span class="op">)</span></span>
<span></span>
<span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">flipper_length_mm</span> <span class="op">~</span> <span class="va">MinMax</span>,</span>
<span>            data <span class="op">=</span> <span class="va">penguins</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_inh1l5nuybrkb80l92g4(i, j, css_id) {
          var table = document.getElementById("tinytable_inh1l5nuybrkb80l92g4");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_inh1l5nuybrkb80l92g4(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_inh1l5nuybrkb80l92g4");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '1', j: 1 },  ], css_id: 'tinytable_css_12aa0hmkxjl2j6zuuacu',}, 
          { positions: [ { i: '0', j: 1 },  ], css_id: 'tinytable_css_08b0gipih5u9hev83ufp',}, 
          { positions: [ { i: '1', j: 0 },  ], css_id: 'tinytable_css_4edlpjvs2f2k1oea06vo',}, 
          { positions: [ { i: '0', j: 0 },  ], css_id: 'tinytable_css_9fle6lzlj777fsycb2rr',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_inh1l5nuybrkb80l92g4(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_12aa0hmkxjl2j6zuuacu, .table th.tinytable_css_12aa0hmkxjl2j6zuuacu { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_08b0gipih5u9hev83ufp, .table th.tinytable_css_08b0gipih5u9hev83ufp { text-align: right; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_4edlpjvs2f2k1oea06vo, .table th.tinytable_css_4edlpjvs2f2k1oea06vo { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_9fle6lzlj777fsycb2rr, .table th.tinytable_css_9fle6lzlj777fsycb2rr { text-align: left; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_inh1l5nuybrkb80l92g4" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead><tr>
<th scope="col" data-row="0" data-col="0"> </th>
                <th scope="col" data-row="0" data-col="1">MinMax</th>
              </tr></thead>
<tbody><tr>
<td data-row="1" data-col="0">flipper_length_mm</td>
                  <td data-row="1" data-col="1">[172, 231]</td>
                </tr></tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
</section><section id="concatenating-with" class="level2"><h2 class="anchored" data-anchor-id="concatenating-with">Concatenating with <code>+</code>
</h2>
<p>To include more rows and columns, we use the <code>+</code> sign:</p>
<div class="cell">
<div class="sourceCode" id="cb17"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">flipper_length_mm</span> <span class="op">+</span> <span class="va">body_mass_g</span> <span class="op">~</span> <span class="va">Mean</span> <span class="op">+</span> <span class="va">SD</span>,</span>
<span>            data <span class="op">=</span> <span class="va">penguins</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_pfm75grirs84xwcorg9z(i, j, css_id) {
          var table = document.getElementById("tinytable_pfm75grirs84xwcorg9z");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_pfm75grirs84xwcorg9z(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_pfm75grirs84xwcorg9z");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '2', j: 1 }, { i: '2', j: 2 },  ], css_id: 'tinytable_css_ms7b83hfwvo0k2yjumwa',}, 
          { positions: [ { i: '1', j: 1 }, { i: '1', j: 2 },  ], css_id: 'tinytable_css_7eejcbdso2qtwqzzkzsc',}, 
          { positions: [ { i: '0', j: 1 }, { i: '0', j: 2 },  ], css_id: 'tinytable_css_m9zhc8q0avaru25ytjj5',}, 
          { positions: [ { i: '2', j: 0 },  ], css_id: 'tinytable_css_lm1gux5yvglcxb9y4qrx',}, 
          { positions: [ { i: '1', j: 0 },  ], css_id: 'tinytable_css_44t7m0i76999ue75js0s',}, 
          { positions: [ { i: '0', j: 0 },  ], css_id: 'tinytable_css_pqivq1gy5xujmmj2u31a',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_pfm75grirs84xwcorg9z(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_ms7b83hfwvo0k2yjumwa, .table th.tinytable_css_ms7b83hfwvo0k2yjumwa { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_7eejcbdso2qtwqzzkzsc, .table th.tinytable_css_7eejcbdso2qtwqzzkzsc { text-align: right; }
      .table td.tinytable_css_m9zhc8q0avaru25ytjj5, .table th.tinytable_css_m9zhc8q0avaru25ytjj5 { text-align: right; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_lm1gux5yvglcxb9y4qrx, .table th.tinytable_css_lm1gux5yvglcxb9y4qrx { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_44t7m0i76999ue75js0s, .table th.tinytable_css_44t7m0i76999ue75js0s { text-align: left; }
      .table td.tinytable_css_pqivq1gy5xujmmj2u31a, .table th.tinytable_css_pqivq1gy5xujmmj2u31a { text-align: left; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_pfm75grirs84xwcorg9z" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead><tr>
<th scope="col" data-row="0" data-col="0"> </th>
                <th scope="col" data-row="0" data-col="1">Mean</th>
                <th scope="col" data-row="0" data-col="2">SD</th>
              </tr></thead>
<tbody>
<tr>
<td data-row="1" data-col="0">flipper_length_mm</td>
                  <td data-row="1" data-col="1">200.92</td>
                  <td data-row="1" data-col="2">14.06</td>
                </tr>
<tr>
<td data-row="2" data-col="0">body_mass_g</td>
                  <td data-row="2" data-col="1">4201.75</td>
                  <td data-row="2" data-col="2">801.95</td>
                </tr>
</tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
<p>Sometimes, it can be cumbersome to list all variables separated by <code>+</code> signs. The <code><a href="https://dmurdoch.github.io/tables/reference/All.html">All()</a></code> function is a useful shortcut:</p>
<div class="cell">
<div class="sourceCode" id="cb18"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="fu"><a href="https://dmurdoch.github.io/tables/reference/All.html">All</a></span><span class="op">(</span><span class="va">penguins</span><span class="op">)</span> <span class="op">~</span> <span class="va">Mean</span> <span class="op">+</span> <span class="va">SD</span>,</span>
<span>            data <span class="op">=</span> <span class="va">penguins</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_aohadbt0hvouz5i1uwdo(i, j, css_id) {
          var table = document.getElementById("tinytable_aohadbt0hvouz5i1uwdo");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_aohadbt0hvouz5i1uwdo(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_aohadbt0hvouz5i1uwdo");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '6', j: 1 }, { i: '6', j: 2 },  ], css_id: 'tinytable_css_tpkqbbfpdnextdan6djp',}, 
          { positions: [ { i: '1', j: 1 }, { i: '2', j: 1 }, { i: '4', j: 1 }, { i: '5', j: 1 }, { i: '3', j: 2 }, { i: '3', j: 1 }, { i: '1', j: 2 }, { i: '2', j: 2 }, { i: '4', j: 2 }, { i: '5', j: 2 },  ], css_id: 'tinytable_css_yenvc6leq2g4s4mdka2w',}, 
          { positions: [ { i: '0', j: 1 }, { i: '0', j: 2 },  ], css_id: 'tinytable_css_2umndbosk1im7l8lmru9',}, 
          { positions: [ { i: '6', j: 0 },  ], css_id: 'tinytable_css_ocq4la7h4xticdbtitjk',}, 
          { positions: [ { i: '1', j: 0 }, { i: '2', j: 0 }, { i: '3', j: 0 }, { i: '4', j: 0 }, { i: '5', j: 0 },  ], css_id: 'tinytable_css_18n3cbanh2ugnwngzf78',}, 
          { positions: [ { i: '0', j: 0 },  ], css_id: 'tinytable_css_n52nz6qzvlv6t01pnzpp',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_aohadbt0hvouz5i1uwdo(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_tpkqbbfpdnextdan6djp, .table th.tinytable_css_tpkqbbfpdnextdan6djp { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_yenvc6leq2g4s4mdka2w, .table th.tinytable_css_yenvc6leq2g4s4mdka2w { text-align: right; }
      .table td.tinytable_css_2umndbosk1im7l8lmru9, .table th.tinytable_css_2umndbosk1im7l8lmru9 { text-align: right; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_ocq4la7h4xticdbtitjk, .table th.tinytable_css_ocq4la7h4xticdbtitjk { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_18n3cbanh2ugnwngzf78, .table th.tinytable_css_18n3cbanh2ugnwngzf78 { text-align: left; }
      .table td.tinytable_css_n52nz6qzvlv6t01pnzpp, .table th.tinytable_css_n52nz6qzvlv6t01pnzpp { text-align: left; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_aohadbt0hvouz5i1uwdo" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead><tr>
<th scope="col" data-row="0" data-col="0"> </th>
                <th scope="col" data-row="0" data-col="1">Mean</th>
                <th scope="col" data-row="0" data-col="2">SD</th>
              </tr></thead>
<tbody>
<tr>
<td data-row="1" data-col="0">rownames</td>
                  <td data-row="1" data-col="1">172.50</td>
                  <td data-row="1" data-col="2">99.45</td>
                </tr>
<tr>
<td data-row="2" data-col="0">bill_length_mm</td>
                  <td data-row="2" data-col="1">43.92</td>
                  <td data-row="2" data-col="2">5.46</td>
                </tr>
<tr>
<td data-row="3" data-col="0">bill_depth_mm</td>
                  <td data-row="3" data-col="1">17.15</td>
                  <td data-row="3" data-col="2">1.97</td>
                </tr>
<tr>
<td data-row="4" data-col="0">flipper_length_mm</td>
                  <td data-row="4" data-col="1">200.92</td>
                  <td data-row="4" data-col="2">14.06</td>
                </tr>
<tr>
<td data-row="5" data-col="0">body_mass_g</td>
                  <td data-row="5" data-col="1">4201.75</td>
                  <td data-row="5" data-col="2">801.95</td>
                </tr>
<tr>
<td data-row="6" data-col="0">year</td>
                  <td data-row="6" data-col="1">2008.03</td>
                  <td data-row="6" data-col="2">0.82</td>
                </tr>
</tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
<p>By default, <code>All</code> selects all numeric variables. This behavior can be changed by modifying the function’s arguments. See <code><a href="https://dmurdoch.github.io/tables/reference/All.html">?All</a></code> for details.</p>
</section><section id="nesting-with" class="level2"><h2 class="anchored" data-anchor-id="nesting-with">Nesting with <code>*</code>
</h2>
<p><code>datasummary</code> can nest variables and statistics inside categorical variables using the <code>*</code> symbol. When applying the the <code>*</code> operator to factor, character, or logical variables, columns or rows will automatically be nested. For instance, if we want to display separate means for each value of the variable <code>sex</code>, we use <code>mean * sex</code>:</p>
<div class="cell">
<div class="sourceCode" id="cb19"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">flipper_length_mm</span> <span class="op">+</span> <span class="va">body_mass_g</span> <span class="op">~</span> <span class="va">mean</span> <span class="op">*</span> <span class="va">sex</span>,</span>
<span>            data <span class="op">=</span> <span class="va">penguins</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_59nl3jiupj18e0lb46f9(i, j, css_id) {
          var table = document.getElementById("tinytable_59nl3jiupj18e0lb46f9");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_59nl3jiupj18e0lb46f9(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_59nl3jiupj18e0lb46f9");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '2', j: 1 }, { i: '2', j: 2 },  ], css_id: 'tinytable_css_ldxie8q3mxh33z1nvlax',}, 
          { positions: [ { i: '1', j: 1 }, { i: '1', j: 2 },  ], css_id: 'tinytable_css_8z9k69zmhz7f06ecwx0k',}, 
          { positions: [ { i: '0', j: 1 }, { i: '0', j: 2 },  ], css_id: 'tinytable_css_9pxpw8niwdm8ea9idst2',}, 
          { positions: [ { i: '2', j: 0 },  ], css_id: 'tinytable_css_gw5czwfv1fxtj9bwctk3',}, 
          { positions: [ { i: '1', j: 0 },  ], css_id: 'tinytable_css_i7b6ewyjyqf5b2fk656w',}, 
          { positions: [ { i: '0', j: 0 },  ], css_id: 'tinytable_css_z9u3kmjdeohu7m2q0u7h',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_59nl3jiupj18e0lb46f9(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_ldxie8q3mxh33z1nvlax, .table th.tinytable_css_ldxie8q3mxh33z1nvlax { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_8z9k69zmhz7f06ecwx0k, .table th.tinytable_css_8z9k69zmhz7f06ecwx0k { text-align: right; }
      .table td.tinytable_css_9pxpw8niwdm8ea9idst2, .table th.tinytable_css_9pxpw8niwdm8ea9idst2 { text-align: right; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_gw5czwfv1fxtj9bwctk3, .table th.tinytable_css_gw5czwfv1fxtj9bwctk3 { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_i7b6ewyjyqf5b2fk656w, .table th.tinytable_css_i7b6ewyjyqf5b2fk656w { text-align: left; }
      .table td.tinytable_css_z9u3kmjdeohu7m2q0u7h, .table th.tinytable_css_z9u3kmjdeohu7m2q0u7h { text-align: left; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_59nl3jiupj18e0lb46f9" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead><tr>
<th scope="col" data-row="0" data-col="0"> </th>
                <th scope="col" data-row="0" data-col="1">female</th>
                <th scope="col" data-row="0" data-col="2">male</th>
              </tr></thead>
<tbody>
<tr>
<td data-row="1" data-col="0">flipper_length_mm</td>
                  <td data-row="1" data-col="1">197.36</td>
                  <td data-row="1" data-col="2">204.51</td>
                </tr>
<tr>
<td data-row="2" data-col="0">body_mass_g</td>
                  <td data-row="2" data-col="1">3862.27</td>
                  <td data-row="2" data-col="2">4545.68</td>
                </tr>
</tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
<p>We can use parentheses to nest several terms inside one another, using a call of this form: <code>x * (y + z)</code>. Here is an example with nested columns:</p>
<div class="cell">
<div class="sourceCode" id="cb20"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">body_mass_g</span> <span class="op">~</span> <span class="va">sex</span> <span class="op">*</span> <span class="op">(</span><span class="va">mean</span> <span class="op">+</span> <span class="va">sd</span><span class="op">)</span>,</span>
<span>            data <span class="op">=</span> <span class="va">penguins</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_m3877zpsd4d85y1o1j6t(i, j, css_id) {
          var table = document.getElementById("tinytable_m3877zpsd4d85y1o1j6t");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_m3877zpsd4d85y1o1j6t(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_m3877zpsd4d85y1o1j6t");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '-1', j: 4 }, { i: '-1', j: 3 },  ], css_id: 'tinytable_css_s0j49m1ncz4pde5kzqgy',}, 
          { positions: [ { i: '1', j: 1 }, { i: '1', j: 4 }, { i: '1', j: 3 }, { i: '1', j: 2 },  ], css_id: 'tinytable_css_ho0z9pcama6l570arz1x',}, 
          { positions: [ { i: '-1', j: 2 }, { i: '-1', j: 1 },  ], css_id: 'tinytable_css_s11r20slid308nkzker6',}, 
          { positions: [ { i: '0', j: 1 }, { i: '0', j: 4 }, { i: '0', j: 3 }, { i: '0', j: 2 },  ], css_id: 'tinytable_css_3ez06l48rwflvxhnzgtf',}, 
          { positions: [ { i: '1', j: 0 },  ], css_id: 'tinytable_css_zci1n7siglsf507cu5z1',}, 
          { positions: [ { i: '-1', j: 0 },  ], css_id: 'tinytable_css_zle3q5g6ft238kn8nm55',}, 
          { positions: [ { i: '0', j: 0 },  ], css_id: 'tinytable_css_cyw9p0se134w58ulbb6n',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_m3877zpsd4d85y1o1j6t(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_s0j49m1ncz4pde5kzqgy, .table th.tinytable_css_s0j49m1ncz4pde5kzqgy { text-align: right; text-align: center; border-top: solid #d3d8dc 0.1em; text-align: center; }
      .table td.tinytable_css_ho0z9pcama6l570arz1x, .table th.tinytable_css_ho0z9pcama6l570arz1x { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_s11r20slid308nkzker6, .table th.tinytable_css_s11r20slid308nkzker6 { text-align: right; text-align: center; border-top: solid #d3d8dc 0.1em; text-align: center; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_3ez06l48rwflvxhnzgtf, .table th.tinytable_css_3ez06l48rwflvxhnzgtf { text-align: right; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_zci1n7siglsf507cu5z1, .table th.tinytable_css_zci1n7siglsf507cu5z1 { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_zle3q5g6ft238kn8nm55, .table th.tinytable_css_zle3q5g6ft238kn8nm55 { text-align: left; text-align: center; border-top: solid #d3d8dc 0.1em; text-align: center; }
      .table td.tinytable_css_cyw9p0se134w58ulbb6n, .table th.tinytable_css_cyw9p0se134w58ulbb6n { text-align: left; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_m3877zpsd4d85y1o1j6t" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead>
<tr>
<th scope="col" align="center" colspan="1" data-row="-1" data-col="0"> </th>
<th scope="col" align="center" colspan="2" data-row="-1" data-col="1">female</th>
<th scope="col" align="center" colspan="2" data-row="-1" data-col="2">male</th>
</tr>
<tr>
<th scope="col" data-row="0" data-col="0"> </th>
                <th scope="col" data-row="0" data-col="1">mean</th>
                <th scope="col" data-row="0" data-col="2">sd</th>
                <th scope="col" data-row="0" data-col="3">mean</th>
                <th scope="col" data-row="0" data-col="4">sd</th>
              </tr>
</thead>
<tbody><tr>
<td data-row="1" data-col="0">body_mass_g</td>
                  <td data-row="1" data-col="1">3862.27</td>
                  <td data-row="1" data-col="2">666.17</td>
                  <td data-row="1" data-col="3">4545.68</td>
                  <td data-row="1" data-col="4">787.63</td>
                </tr></tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
<p>Here is an example with nested rows:</p>
<div class="cell">
<div class="sourceCode" id="cb21"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">sex</span> <span class="op">*</span> <span class="op">(</span><span class="va">body_mass_g</span> <span class="op">+</span> <span class="va">flipper_length_mm</span><span class="op">)</span> <span class="op">~</span> <span class="va">mean</span> <span class="op">+</span> <span class="va">sd</span>,</span>
<span>            data <span class="op">=</span> <span class="va">penguins</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_3veljfj834at9izgnr8q(i, j, css_id) {
          var table = document.getElementById("tinytable_3veljfj834at9izgnr8q");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_3veljfj834at9izgnr8q(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_3veljfj834at9izgnr8q");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '4', j: 2 }, { i: '4', j: 3 },  ], css_id: 'tinytable_css_ic79jypd0i0co0x22yjs',}, 
          { positions: [ { i: '2', j: 2 }, { i: '3', j: 2 }, { i: '3', j: 3 }, { i: '1', j: 2 }, { i: '1', j: 3 }, { i: '2', j: 3 },  ], css_id: 'tinytable_css_0gs25fdlxx1fsn51806m',}, 
          { positions: [ { i: '0', j: 2 }, { i: '0', j: 3 },  ], css_id: 'tinytable_css_sgpkpvdrbg2cozaup961',}, 
          { positions: [ { i: '4', j: 0 }, { i: '4', j: 1 },  ], css_id: 'tinytable_css_tyb87bzu2anwkc3bgu16',}, 
          { positions: [ { i: '2', j: 0 }, { i: '3', j: 0 }, { i: '3', j: 1 }, { i: '1', j: 0 }, { i: '1', j: 1 }, { i: '2', j: 1 },  ], css_id: 'tinytable_css_pcv8y7sjje635ibz8l0j',}, 
          { positions: [ { i: '0', j: 0 }, { i: '0', j: 1 },  ], css_id: 'tinytable_css_krv5zjqx3qkispfnv9bu',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_3veljfj834at9izgnr8q(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_ic79jypd0i0co0x22yjs, .table th.tinytable_css_ic79jypd0i0co0x22yjs { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_0gs25fdlxx1fsn51806m, .table th.tinytable_css_0gs25fdlxx1fsn51806m { text-align: right; }
      .table td.tinytable_css_sgpkpvdrbg2cozaup961, .table th.tinytable_css_sgpkpvdrbg2cozaup961 { text-align: right; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_tyb87bzu2anwkc3bgu16, .table th.tinytable_css_tyb87bzu2anwkc3bgu16 { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_pcv8y7sjje635ibz8l0j, .table th.tinytable_css_pcv8y7sjje635ibz8l0j { text-align: left; }
      .table td.tinytable_css_krv5zjqx3qkispfnv9bu, .table th.tinytable_css_krv5zjqx3qkispfnv9bu { text-align: left; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_3veljfj834at9izgnr8q" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead><tr>
<th scope="col" data-row="0" data-col="0">sex</th>
                <th scope="col" data-row="0" data-col="1"> </th>
                <th scope="col" data-row="0" data-col="2">mean</th>
                <th scope="col" data-row="0" data-col="3">sd</th>
              </tr></thead>
<tbody>
<tr>
<td data-row="1" data-col="0">female</td>
                  <td data-row="1" data-col="1">body_mass_g</td>
                  <td data-row="1" data-col="2">3862.27</td>
                  <td data-row="1" data-col="3">666.17</td>
                </tr>
<tr>
<td data-row="2" data-col="0"></td>
                  <td data-row="2" data-col="1">flipper_length_mm</td>
                  <td data-row="2" data-col="2">197.36</td>
                  <td data-row="2" data-col="3">12.50</td>
                </tr>
<tr>
<td data-row="3" data-col="0">male</td>
                  <td data-row="3" data-col="1">body_mass_g</td>
                  <td data-row="3" data-col="2">4545.68</td>
                  <td data-row="3" data-col="3">787.63</td>
                </tr>
<tr>
<td data-row="4" data-col="0"></td>
                  <td data-row="4" data-col="1">flipper_length_mm</td>
                  <td data-row="4" data-col="2">204.51</td>
                  <td data-row="4" data-col="3">14.55</td>
                </tr>
</tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
<p>The order in which terms enter the formula determines the order in which labels are displayed. For example, this shows <code>island</code> above <code>sex</code>:</p>
<div class="cell">
<div class="sourceCode" id="cb22"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">flipper_length_mm</span> <span class="op">+</span> <span class="va">body_mass_g</span> <span class="op">~</span> <span class="va">mean</span> <span class="op">*</span> <span class="va">island</span> <span class="op">*</span> <span class="va">sex</span>,</span>
<span>            data <span class="op">=</span> <span class="va">penguins</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_6j2dwg8d8b1stwf1i4ni(i, j, css_id) {
          var table = document.getElementById("tinytable_6j2dwg8d8b1stwf1i4ni");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_6j2dwg8d8b1stwf1i4ni(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_6j2dwg8d8b1stwf1i4ni");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '-1', j: 4 }, { i: '-1', j: 5 }, { i: '-1', j: 6 },  ], css_id: 'tinytable_css_un942l9w8oicrf1vow3d',}, 
          { positions: [ { i: '2', j: 1 }, { i: '2', j: 2 }, { i: '2', j: 3 }, { i: '2', j: 4 }, { i: '2', j: 5 }, { i: '2', j: 6 },  ], css_id: 'tinytable_css_msgo1fzbw9dkvnsrt61f',}, 
          { positions: [ { i: '1', j: 1 }, { i: '1', j: 2 }, { i: '1', j: 3 }, { i: '1', j: 4 }, { i: '1', j: 5 }, { i: '1', j: 6 },  ], css_id: 'tinytable_css_xni1rcr9l45jqcmideuc',}, 
          { positions: [ { i: '-1', j: 1 }, { i: '-1', j: 2 }, { i: '-1', j: 3 },  ], css_id: 'tinytable_css_2d6gdumg6wt8q1lk2vdz',}, 
          { positions: [ { i: '0', j: 1 }, { i: '0', j: 2 }, { i: '0', j: 3 }, { i: '0', j: 4 }, { i: '0', j: 5 }, { i: '0', j: 6 },  ], css_id: 'tinytable_css_s1qor3are62jmyqapxna',}, 
          { positions: [ { i: '2', j: 0 },  ], css_id: 'tinytable_css_4n9v8vxsoxupkthpgjxp',}, 
          { positions: [ { i: '1', j: 0 },  ], css_id: 'tinytable_css_lxpezq1h0kny10x7de6l',}, 
          { positions: [ { i: '-1', j: 0 },  ], css_id: 'tinytable_css_cbjeup54zn2thcf9sx92',}, 
          { positions: [ { i: '0', j: 0 },  ], css_id: 'tinytable_css_j204yhb1h36m3xb9f5tk',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_6j2dwg8d8b1stwf1i4ni(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_un942l9w8oicrf1vow3d, .table th.tinytable_css_un942l9w8oicrf1vow3d { text-align: right; text-align: center; border-top: solid #d3d8dc 0.1em; text-align: center; }
      .table td.tinytable_css_msgo1fzbw9dkvnsrt61f, .table th.tinytable_css_msgo1fzbw9dkvnsrt61f { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_xni1rcr9l45jqcmideuc, .table th.tinytable_css_xni1rcr9l45jqcmideuc { text-align: right; }
      .table td.tinytable_css_2d6gdumg6wt8q1lk2vdz, .table th.tinytable_css_2d6gdumg6wt8q1lk2vdz { text-align: right; text-align: center; border-top: solid #d3d8dc 0.1em; text-align: center; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_s1qor3are62jmyqapxna, .table th.tinytable_css_s1qor3are62jmyqapxna { text-align: right; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_4n9v8vxsoxupkthpgjxp, .table th.tinytable_css_4n9v8vxsoxupkthpgjxp { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_lxpezq1h0kny10x7de6l, .table th.tinytable_css_lxpezq1h0kny10x7de6l { text-align: left; }
      .table td.tinytable_css_cbjeup54zn2thcf9sx92, .table th.tinytable_css_cbjeup54zn2thcf9sx92 { text-align: left; text-align: center; border-top: solid #d3d8dc 0.1em; text-align: center; }
      .table td.tinytable_css_j204yhb1h36m3xb9f5tk, .table th.tinytable_css_j204yhb1h36m3xb9f5tk { text-align: left; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_6j2dwg8d8b1stwf1i4ni" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead>
<tr>
<th scope="col" align="center" colspan="1" data-row="-1" data-col="0"> </th>
<th scope="col" align="center" colspan="2" data-row="-1" data-col="1">Biscoe</th>
<th scope="col" align="center" colspan="2" data-row="-1" data-col="2">Dream</th>
<th scope="col" align="center" colspan="2" data-row="-1" data-col="3">Torgersen</th>
</tr>
<tr>
<th scope="col" data-row="0" data-col="0"> </th>
                <th scope="col" data-row="0" data-col="1">female</th>
                <th scope="col" data-row="0" data-col="2">male</th>
                <th scope="col" data-row="0" data-col="3">female</th>
                <th scope="col" data-row="0" data-col="4">male</th>
                <th scope="col" data-row="0" data-col="5">female</th>
                <th scope="col" data-row="0" data-col="6">male</th>
              </tr>
</thead>
<tbody>
<tr>
<td data-row="1" data-col="0">flipper_length_mm</td>
                  <td data-row="1" data-col="1">205.69</td>
                  <td data-row="1" data-col="2">213.29</td>
                  <td data-row="1" data-col="3">190.02</td>
                  <td data-row="1" data-col="4">196.31</td>
                  <td data-row="1" data-col="5">188.29</td>
                  <td data-row="1" data-col="6">194.91</td>
                </tr>
<tr>
<td data-row="2" data-col="0">body_mass_g</td>
                  <td data-row="2" data-col="1">4319.38</td>
                  <td data-row="2" data-col="2">5104.52</td>
                  <td data-row="2" data-col="3">3446.31</td>
                  <td data-row="2" data-col="4">3987.10</td>
                  <td data-row="2" data-col="5">3395.83</td>
                  <td data-row="2" data-col="6">4034.78</td>
                </tr>
</tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
<p>This shows <code>sex</code> above <code>island</code> values:</p>
<div class="cell">
<div class="sourceCode" id="cb23"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">flipper_length_mm</span> <span class="op">+</span> <span class="va">body_mass_g</span> <span class="op">~</span> <span class="va">mean</span> <span class="op">*</span> <span class="va">sex</span> <span class="op">*</span> <span class="va">island</span>,</span>
<span>            data <span class="op">=</span> <span class="va">penguins</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_ipnhrdv14wiew1jw5zqz(i, j, css_id) {
          var table = document.getElementById("tinytable_ipnhrdv14wiew1jw5zqz");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_ipnhrdv14wiew1jw5zqz(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_ipnhrdv14wiew1jw5zqz");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '-1', j: 3 }, { i: '-1', j: 4 }, { i: '-1', j: 5 }, { i: '-1', j: 6 },  ], css_id: 'tinytable_css_0j2bkkd38736q4p0zgvy',}, 
          { positions: [ { i: '2', j: 1 }, { i: '2', j: 2 }, { i: '2', j: 3 }, { i: '2', j: 4 }, { i: '2', j: 5 }, { i: '2', j: 6 },  ], css_id: 'tinytable_css_pt2zmahdzyzvdkk491fz',}, 
          { positions: [ { i: '1', j: 1 }, { i: '1', j: 2 }, { i: '1', j: 3 }, { i: '1', j: 4 }, { i: '1', j: 5 }, { i: '1', j: 6 },  ], css_id: 'tinytable_css_w3rlhu9vssdidj1j6zy6',}, 
          { positions: [ { i: '-1', j: 1 }, { i: '-1', j: 2 },  ], css_id: 'tinytable_css_mflozxo9yg6kavjgoiyv',}, 
          { positions: [ { i: '0', j: 1 }, { i: '0', j: 2 }, { i: '0', j: 3 }, { i: '0', j: 4 }, { i: '0', j: 5 }, { i: '0', j: 6 },  ], css_id: 'tinytable_css_2x0luzwr119gf2l3v90v',}, 
          { positions: [ { i: '2', j: 0 },  ], css_id: 'tinytable_css_ieepfa4hgts20citauuf',}, 
          { positions: [ { i: '1', j: 0 },  ], css_id: 'tinytable_css_ews2m5zubr97luf8ukbn',}, 
          { positions: [ { i: '-1', j: 0 },  ], css_id: 'tinytable_css_ffd0v1ulfvpymd0cfobn',}, 
          { positions: [ { i: '0', j: 0 },  ], css_id: 'tinytable_css_euawdloi7qlltc6tna35',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_ipnhrdv14wiew1jw5zqz(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_0j2bkkd38736q4p0zgvy, .table th.tinytable_css_0j2bkkd38736q4p0zgvy { text-align: right; text-align: center; border-top: solid #d3d8dc 0.1em; text-align: center; }
      .table td.tinytable_css_pt2zmahdzyzvdkk491fz, .table th.tinytable_css_pt2zmahdzyzvdkk491fz { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_w3rlhu9vssdidj1j6zy6, .table th.tinytable_css_w3rlhu9vssdidj1j6zy6 { text-align: right; }
      .table td.tinytable_css_mflozxo9yg6kavjgoiyv, .table th.tinytable_css_mflozxo9yg6kavjgoiyv { text-align: right; text-align: center; border-top: solid #d3d8dc 0.1em; text-align: center; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_2x0luzwr119gf2l3v90v, .table th.tinytable_css_2x0luzwr119gf2l3v90v { text-align: right; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_ieepfa4hgts20citauuf, .table th.tinytable_css_ieepfa4hgts20citauuf { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_ews2m5zubr97luf8ukbn, .table th.tinytable_css_ews2m5zubr97luf8ukbn { text-align: left; }
      .table td.tinytable_css_ffd0v1ulfvpymd0cfobn, .table th.tinytable_css_ffd0v1ulfvpymd0cfobn { text-align: left; text-align: center; border-top: solid #d3d8dc 0.1em; text-align: center; }
      .table td.tinytable_css_euawdloi7qlltc6tna35, .table th.tinytable_css_euawdloi7qlltc6tna35 { text-align: left; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_ipnhrdv14wiew1jw5zqz" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead>
<tr>
<th scope="col" align="center" colspan="1" data-row="-1" data-col="0"> </th>
<th scope="col" align="center" colspan="3" data-row="-1" data-col="1">female</th>
<th scope="col" align="center" colspan="3" data-row="-1" data-col="2">male</th>
</tr>
<tr>
<th scope="col" data-row="0" data-col="0"> </th>
                <th scope="col" data-row="0" data-col="1">Biscoe</th>
                <th scope="col" data-row="0" data-col="2">Dream</th>
                <th scope="col" data-row="0" data-col="3">Torgersen</th>
                <th scope="col" data-row="0" data-col="4">Biscoe</th>
                <th scope="col" data-row="0" data-col="5">Dream</th>
                <th scope="col" data-row="0" data-col="6">Torgersen</th>
              </tr>
</thead>
<tbody>
<tr>
<td data-row="1" data-col="0">flipper_length_mm</td>
                  <td data-row="1" data-col="1">205.69</td>
                  <td data-row="1" data-col="2">190.02</td>
                  <td data-row="1" data-col="3">188.29</td>
                  <td data-row="1" data-col="4">213.29</td>
                  <td data-row="1" data-col="5">196.31</td>
                  <td data-row="1" data-col="6">194.91</td>
                </tr>
<tr>
<td data-row="2" data-col="0">body_mass_g</td>
                  <td data-row="2" data-col="1">4319.38</td>
                  <td data-row="2" data-col="2">3446.31</td>
                  <td data-row="2" data-col="3">3395.83</td>
                  <td data-row="2" data-col="4">5104.52</td>
                  <td data-row="2" data-col="5">3987.10</td>
                  <td data-row="2" data-col="6">4034.78</td>
                </tr>
</tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
<p>By default, <code>datasummary</code> omits column headers with a single value/label across all columns, except for the header that sits just above the data. If the header we want to see is not displayed, we may want to reorder the terms of the formula. To show all headers, set <code>sparse_header=FALSE</code>:</p>
<div class="cell">
<div class="sourceCode" id="cb24"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">flipper_length_mm</span> <span class="op">+</span> <span class="va">body_mass_g</span> <span class="op">~</span> <span class="va">mean</span> <span class="op">*</span> <span class="va">sex</span> <span class="op">*</span> <span class="va">island</span>,</span>
<span>            data <span class="op">=</span> <span class="va">penguins</span>,</span>
<span>            sparse_header <span class="op">=</span> <span class="cn">FALSE</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_0cudbdarsexrctj688n7(i, j, css_id) {
          var table = document.getElementById("tinytable_0cudbdarsexrctj688n7");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_0cudbdarsexrctj688n7(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_0cudbdarsexrctj688n7");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '-1', j: 3 }, { i: '-1', j: 4 }, { i: '-1', j: 5 }, { i: '-1', j: 6 },  ], css_id: 'tinytable_css_5l1sij57gia8d1wsm1kz',}, 
          { positions: [ { i: '2', j: 1 }, { i: '2', j: 2 }, { i: '2', j: 3 }, { i: '2', j: 4 }, { i: '2', j: 5 }, { i: '2', j: 6 },  ], css_id: 'tinytable_css_jg1930a2fzb8kssaa2vs',}, 
          { positions: [ { i: '1', j: 1 }, { i: '1', j: 2 }, { i: '1', j: 3 }, { i: '1', j: 4 }, { i: '1', j: 5 }, { i: '1', j: 6 },  ], css_id: 'tinytable_css_rtv8ch1amvjz8m6brsfz',}, 
          { positions: [ { i: '-1', j: 1 }, { i: '-1', j: 2 },  ], css_id: 'tinytable_css_2l2vrar3j7d8tb3jf1ja',}, 
          { positions: [ { i: '0', j: 1 }, { i: '0', j: 2 }, { i: '0', j: 3 }, { i: '0', j: 4 }, { i: '0', j: 5 }, { i: '0', j: 6 },  ], css_id: 'tinytable_css_czt9q957l0hjqivrpgiv',}, 
          { positions: [ { i: '2', j: 0 },  ], css_id: 'tinytable_css_tt8qh9dmreer99azva1e',}, 
          { positions: [ { i: '1', j: 0 },  ], css_id: 'tinytable_css_64ntws4idcj57nirz7lh',}, 
          { positions: [ { i: '-1', j: 0 },  ], css_id: 'tinytable_css_roncd1rjfilter234fwq',}, 
          { positions: [ { i: '0', j: 0 },  ], css_id: 'tinytable_css_julbvfnki1egun8sk3ow',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_0cudbdarsexrctj688n7(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_5l1sij57gia8d1wsm1kz, .table th.tinytable_css_5l1sij57gia8d1wsm1kz { text-align: right; text-align: center; border-top: solid #d3d8dc 0.1em; text-align: center; }
      .table td.tinytable_css_jg1930a2fzb8kssaa2vs, .table th.tinytable_css_jg1930a2fzb8kssaa2vs { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_rtv8ch1amvjz8m6brsfz, .table th.tinytable_css_rtv8ch1amvjz8m6brsfz { text-align: right; }
      .table td.tinytable_css_2l2vrar3j7d8tb3jf1ja, .table th.tinytable_css_2l2vrar3j7d8tb3jf1ja { text-align: right; text-align: center; border-top: solid #d3d8dc 0.1em; text-align: center; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_czt9q957l0hjqivrpgiv, .table th.tinytable_css_czt9q957l0hjqivrpgiv { text-align: right; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_tt8qh9dmreer99azva1e, .table th.tinytable_css_tt8qh9dmreer99azva1e { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_64ntws4idcj57nirz7lh, .table th.tinytable_css_64ntws4idcj57nirz7lh { text-align: left; }
      .table td.tinytable_css_roncd1rjfilter234fwq, .table th.tinytable_css_roncd1rjfilter234fwq { text-align: left; text-align: center; border-top: solid #d3d8dc 0.1em; text-align: center; }
      .table td.tinytable_css_julbvfnki1egun8sk3ow, .table th.tinytable_css_julbvfnki1egun8sk3ow { text-align: left; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_0cudbdarsexrctj688n7" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead>
<tr>
<th scope="col" align="center" colspan="1" data-row="-1" data-col="0"> </th>
<th scope="col" align="center" colspan="3" data-row="-1" data-col="1">female</th>
<th scope="col" align="center" colspan="3" data-row="-1" data-col="2">male</th>
</tr>
<tr>
<th scope="col" data-row="0" data-col="0"> </th>
                <th scope="col" data-row="0" data-col="1">Biscoe</th>
                <th scope="col" data-row="0" data-col="2">Dream</th>
                <th scope="col" data-row="0" data-col="3">Torgersen</th>
                <th scope="col" data-row="0" data-col="4">Biscoe</th>
                <th scope="col" data-row="0" data-col="5">Dream</th>
                <th scope="col" data-row="0" data-col="6">Torgersen</th>
              </tr>
</thead>
<tbody>
<tr>
<td data-row="1" data-col="0">flipper_length_mm</td>
                  <td data-row="1" data-col="1">205.69</td>
                  <td data-row="1" data-col="2">190.02</td>
                  <td data-row="1" data-col="3">188.29</td>
                  <td data-row="1" data-col="4">213.29</td>
                  <td data-row="1" data-col="5">196.31</td>
                  <td data-row="1" data-col="6">194.91</td>
                </tr>
<tr>
<td data-row="2" data-col="0">body_mass_g</td>
                  <td data-row="2" data-col="1">4319.38</td>
                  <td data-row="2" data-col="2">3446.31</td>
                  <td data-row="2" data-col="3">3395.83</td>
                  <td data-row="2" data-col="4">5104.52</td>
                  <td data-row="2" data-col="5">3987.10</td>
                  <td data-row="2" data-col="6">4034.78</td>
                </tr>
</tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
<p>When using <code>sparse_header=FALSE</code>, it is often useful to insert <code>Heading() *</code> in the table formula, in order to rename or omit some of the labels manually. Type <code><a href="https://dmurdoch.github.io/tables/reference/Heading.html">?tables::Heading</a></code> for details and examples.</p>
</section><section id="renaming-with" class="level2"><h2 class="anchored" data-anchor-id="renaming-with">Renaming with <code>=</code>
</h2>
<p>Personally, I prefer to rename variables and values <em>before</em> drawing my tables, using backticks when variable names include whitespace. For example,</p>
<div class="cell">
<div class="sourceCode" id="cb25"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va">tmp</span> <span class="op">&lt;-</span> <span class="va">penguins</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html">%&gt;%</a></span></span>
<span>       <span class="fu"><a href="https://dplyr.tidyverse.org/reference/select.html">select</a></span><span class="op">(</span>`Flipper length (mm)` <span class="op">=</span> <span class="va">flipper_length_mm</span>,</span>
<span>              `Body mass (g)` <span class="op">=</span> <span class="va">body_mass_g</span><span class="op">)</span></span>
<span></span>
<span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">`Flipper length (mm)`</span> <span class="op">+</span> <span class="va">`Body mass (g)`</span> <span class="op">~</span> <span class="va">Mean</span> <span class="op">+</span> <span class="va">SD</span>,</span>
<span>            data <span class="op">=</span> <span class="va">tmp</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_0s133rooi1c2p4y7k1b4(i, j, css_id) {
          var table = document.getElementById("tinytable_0s133rooi1c2p4y7k1b4");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_0s133rooi1c2p4y7k1b4(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_0s133rooi1c2p4y7k1b4");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '2', j: 1 }, { i: '2', j: 2 },  ], css_id: 'tinytable_css_dn6ucamk18y3g24h5608',}, 
          { positions: [ { i: '1', j: 1 }, { i: '1', j: 2 },  ], css_id: 'tinytable_css_no1p0o4i6oosspsa3mhl',}, 
          { positions: [ { i: '0', j: 1 }, { i: '0', j: 2 },  ], css_id: 'tinytable_css_uk478tr28yvudrdn6p14',}, 
          { positions: [ { i: '2', j: 0 },  ], css_id: 'tinytable_css_x77tt41zh47jv2puprta',}, 
          { positions: [ { i: '1', j: 0 },  ], css_id: 'tinytable_css_icztixh60efbekeajsax',}, 
          { positions: [ { i: '0', j: 0 },  ], css_id: 'tinytable_css_gql3xhxxu2xfef61l40x',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_0s133rooi1c2p4y7k1b4(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_dn6ucamk18y3g24h5608, .table th.tinytable_css_dn6ucamk18y3g24h5608 { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_no1p0o4i6oosspsa3mhl, .table th.tinytable_css_no1p0o4i6oosspsa3mhl { text-align: right; }
      .table td.tinytable_css_uk478tr28yvudrdn6p14, .table th.tinytable_css_uk478tr28yvudrdn6p14 { text-align: right; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_x77tt41zh47jv2puprta, .table th.tinytable_css_x77tt41zh47jv2puprta { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_icztixh60efbekeajsax, .table th.tinytable_css_icztixh60efbekeajsax { text-align: left; }
      .table td.tinytable_css_gql3xhxxu2xfef61l40x, .table th.tinytable_css_gql3xhxxu2xfef61l40x { text-align: left; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_0s133rooi1c2p4y7k1b4" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead><tr>
<th scope="col" data-row="0" data-col="0"> </th>
                <th scope="col" data-row="0" data-col="1">Mean</th>
                <th scope="col" data-row="0" data-col="2">SD</th>
              </tr></thead>
<tbody>
<tr>
<td data-row="1" data-col="0">Flipper length (mm)</td>
                  <td data-row="1" data-col="1">200.92</td>
                  <td data-row="1" data-col="2">14.06</td>
                </tr>
<tr>
<td data-row="2" data-col="0">Body mass (g)</td>
                  <td data-row="2" data-col="1">4201.75</td>
                  <td data-row="2" data-col="2">801.95</td>
                </tr>
</tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
<p>However, thanks to the <code>tables</code> package, <code>datasummary</code> offers two additional mechanisms to rename. First, we can wrap a term in parentheses and use the equal <code>=</code> sign: <code>(NewName=OldName)</code>:</p>
<div class="cell">
<div class="sourceCode" id="cb26"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="op">(</span><span class="va">`Flipper length (mm)`</span> <span class="op">=</span> <span class="va">flipper_length_mm</span><span class="op">)</span> <span class="op">+</span> <span class="op">(</span><span class="va">`Body mass (g)`</span> <span class="op">=</span> <span class="va">body_mass_g</span><span class="op">)</span> <span class="op">~</span></span>
<span>            <span class="va">island</span> <span class="op">*</span> <span class="op">(</span><span class="op">(</span><span class="va">Avg.</span> <span class="op">=</span> <span class="va">Mean</span><span class="op">)</span> <span class="op">+</span> <span class="op">(</span><span class="va">Std.Dev.</span> <span class="op">=</span> <span class="va">SD</span><span class="op">)</span><span class="op">)</span>,</span>
<span>            data <span class="op">=</span> <span class="va">penguins</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_anb4s4yxvmmrlo4hxkbg(i, j, css_id) {
          var table = document.getElementById("tinytable_anb4s4yxvmmrlo4hxkbg");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_anb4s4yxvmmrlo4hxkbg(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_anb4s4yxvmmrlo4hxkbg");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '-1', j: 4 }, { i: '-1', j: 5 }, { i: '-1', j: 6 },  ], css_id: 'tinytable_css_4cgg6t2ymduc8al2pdbh',}, 
          { positions: [ { i: '2', j: 1 }, { i: '2', j: 2 }, { i: '2', j: 3 }, { i: '2', j: 4 }, { i: '2', j: 5 }, { i: '2', j: 6 },  ], css_id: 'tinytable_css_zimsmhql7soqyatrm248',}, 
          { positions: [ { i: '1', j: 1 }, { i: '1', j: 2 }, { i: '1', j: 3 }, { i: '1', j: 4 }, { i: '1', j: 5 }, { i: '1', j: 6 },  ], css_id: 'tinytable_css_n2e752n9cjq8uafyb031',}, 
          { positions: [ { i: '-1', j: 1 }, { i: '-1', j: 2 }, { i: '-1', j: 3 },  ], css_id: 'tinytable_css_4arpqnm3homc0r8uem9t',}, 
          { positions: [ { i: '0', j: 1 }, { i: '0', j: 2 }, { i: '0', j: 3 }, { i: '0', j: 4 }, { i: '0', j: 5 }, { i: '0', j: 6 },  ], css_id: 'tinytable_css_z1nvewlnspnd887kvs0t',}, 
          { positions: [ { i: '2', j: 0 },  ], css_id: 'tinytable_css_voqxhifayofw77dylphv',}, 
          { positions: [ { i: '1', j: 0 },  ], css_id: 'tinytable_css_7qmv64i9ge8ppba4y0wd',}, 
          { positions: [ { i: '-1', j: 0 },  ], css_id: 'tinytable_css_khqd7630wknp8rzsaftd',}, 
          { positions: [ { i: '0', j: 0 },  ], css_id: 'tinytable_css_w1qx83rbuk522vcsnees',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_anb4s4yxvmmrlo4hxkbg(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_4cgg6t2ymduc8al2pdbh, .table th.tinytable_css_4cgg6t2ymduc8al2pdbh { text-align: right; text-align: center; border-top: solid #d3d8dc 0.1em; text-align: center; }
      .table td.tinytable_css_zimsmhql7soqyatrm248, .table th.tinytable_css_zimsmhql7soqyatrm248 { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_n2e752n9cjq8uafyb031, .table th.tinytable_css_n2e752n9cjq8uafyb031 { text-align: right; }
      .table td.tinytable_css_4arpqnm3homc0r8uem9t, .table th.tinytable_css_4arpqnm3homc0r8uem9t { text-align: right; text-align: center; border-top: solid #d3d8dc 0.1em; text-align: center; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_z1nvewlnspnd887kvs0t, .table th.tinytable_css_z1nvewlnspnd887kvs0t { text-align: right; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_voqxhifayofw77dylphv, .table th.tinytable_css_voqxhifayofw77dylphv { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_7qmv64i9ge8ppba4y0wd, .table th.tinytable_css_7qmv64i9ge8ppba4y0wd { text-align: left; }
      .table td.tinytable_css_khqd7630wknp8rzsaftd, .table th.tinytable_css_khqd7630wknp8rzsaftd { text-align: left; text-align: center; border-top: solid #d3d8dc 0.1em; text-align: center; }
      .table td.tinytable_css_w1qx83rbuk522vcsnees, .table th.tinytable_css_w1qx83rbuk522vcsnees { text-align: left; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_anb4s4yxvmmrlo4hxkbg" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead>
<tr>
<th scope="col" align="center" colspan="1" data-row="-1" data-col="0"> </th>
<th scope="col" align="center" colspan="2" data-row="-1" data-col="1">Biscoe</th>
<th scope="col" align="center" colspan="2" data-row="-1" data-col="2">Dream</th>
<th scope="col" align="center" colspan="2" data-row="-1" data-col="3">Torgersen</th>
</tr>
<tr>
<th scope="col" data-row="0" data-col="0"> </th>
                <th scope="col" data-row="0" data-col="1">Avg.</th>
                <th scope="col" data-row="0" data-col="2">Std.Dev.</th>
                <th scope="col" data-row="0" data-col="3">Avg.</th>
                <th scope="col" data-row="0" data-col="4">Std.Dev.</th>
                <th scope="col" data-row="0" data-col="5">Avg.</th>
                <th scope="col" data-row="0" data-col="6">Std.Dev.</th>
              </tr>
</thead>
<tbody>
<tr>
<td data-row="1" data-col="0">Flipper length (mm)</td>
                  <td data-row="1" data-col="1">209.71</td>
                  <td data-row="1" data-col="2">14.14</td>
                  <td data-row="1" data-col="3">193.07</td>
                  <td data-row="1" data-col="4">7.51</td>
                  <td data-row="1" data-col="5">191.20</td>
                  <td data-row="1" data-col="6">6.23</td>
                </tr>
<tr>
<td data-row="2" data-col="0">Body mass (g)</td>
                  <td data-row="2" data-col="1">4716.02</td>
                  <td data-row="2" data-col="2">782.86</td>
                  <td data-row="2" data-col="3">3712.90</td>
                  <td data-row="2" data-col="4">416.64</td>
                  <td data-row="2" data-col="5">3706.37</td>
                  <td data-row="2" data-col="6">445.11</td>
                </tr>
</tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
<p>Second, we can use the <code><a href="https://dmurdoch.github.io/tables/reference/Heading.html">Heading()</a></code> function:</p>
<div class="cell">
<div class="sourceCode" id="cb27"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="fu"><a href="https://dmurdoch.github.io/tables/reference/Heading.html">Heading</a></span><span class="op">(</span><span class="st">"Flipper length (mm)"</span><span class="op">)</span> <span class="op">*</span> <span class="va">flipper_length_mm</span> <span class="op">+</span> <span class="fu"><a href="https://dmurdoch.github.io/tables/reference/Heading.html">Heading</a></span><span class="op">(</span><span class="st">"Body mass (g)"</span><span class="op">)</span> <span class="op">*</span> <span class="va">body_mass_g</span> <span class="op">~</span> <span class="va">island</span> <span class="op">*</span> <span class="op">(</span><span class="va">Mean</span> <span class="op">+</span> <span class="va">SD</span><span class="op">)</span>,</span>
<span>            data <span class="op">=</span> <span class="va">penguins</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_ub49w7cfr2l66385lpb9(i, j, css_id) {
          var table = document.getElementById("tinytable_ub49w7cfr2l66385lpb9");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_ub49w7cfr2l66385lpb9(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_ub49w7cfr2l66385lpb9");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '-1', j: 4 }, { i: '-1', j: 5 }, { i: '-1', j: 6 },  ], css_id: 'tinytable_css_bu75fb58gr3qz3yyug2n',}, 
          { positions: [ { i: '2', j: 1 }, { i: '2', j: 2 }, { i: '2', j: 3 }, { i: '2', j: 4 }, { i: '2', j: 5 }, { i: '2', j: 6 },  ], css_id: 'tinytable_css_cawl8sh7y31k07am9zvn',}, 
          { positions: [ { i: '1', j: 1 }, { i: '1', j: 2 }, { i: '1', j: 3 }, { i: '1', j: 4 }, { i: '1', j: 5 }, { i: '1', j: 6 },  ], css_id: 'tinytable_css_t04e8umt5rwj6ayqnxz5',}, 
          { positions: [ { i: '-1', j: 1 }, { i: '-1', j: 2 }, { i: '-1', j: 3 },  ], css_id: 'tinytable_css_r16pr88343p788m7rbjp',}, 
          { positions: [ { i: '0', j: 1 }, { i: '0', j: 2 }, { i: '0', j: 3 }, { i: '0', j: 4 }, { i: '0', j: 5 }, { i: '0', j: 6 },  ], css_id: 'tinytable_css_ww5fxv30eza1vdtelmho',}, 
          { positions: [ { i: '2', j: 0 },  ], css_id: 'tinytable_css_4x4rzg1j00ood48cgefz',}, 
          { positions: [ { i: '1', j: 0 },  ], css_id: 'tinytable_css_lmpiu649hqcg6rlb2rzk',}, 
          { positions: [ { i: '-1', j: 0 },  ], css_id: 'tinytable_css_zqw4hygzf1sk5gphs3qk',}, 
          { positions: [ { i: '0', j: 0 },  ], css_id: 'tinytable_css_lc27ag893lie66w9vvqg',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_ub49w7cfr2l66385lpb9(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_bu75fb58gr3qz3yyug2n, .table th.tinytable_css_bu75fb58gr3qz3yyug2n { text-align: right; text-align: center; border-top: solid #d3d8dc 0.1em; text-align: center; }
      .table td.tinytable_css_cawl8sh7y31k07am9zvn, .table th.tinytable_css_cawl8sh7y31k07am9zvn { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_t04e8umt5rwj6ayqnxz5, .table th.tinytable_css_t04e8umt5rwj6ayqnxz5 { text-align: right; }
      .table td.tinytable_css_r16pr88343p788m7rbjp, .table th.tinytable_css_r16pr88343p788m7rbjp { text-align: right; text-align: center; border-top: solid #d3d8dc 0.1em; text-align: center; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_ww5fxv30eza1vdtelmho, .table th.tinytable_css_ww5fxv30eza1vdtelmho { text-align: right; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_4x4rzg1j00ood48cgefz, .table th.tinytable_css_4x4rzg1j00ood48cgefz { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_lmpiu649hqcg6rlb2rzk, .table th.tinytable_css_lmpiu649hqcg6rlb2rzk { text-align: left; }
      .table td.tinytable_css_zqw4hygzf1sk5gphs3qk, .table th.tinytable_css_zqw4hygzf1sk5gphs3qk { text-align: left; text-align: center; border-top: solid #d3d8dc 0.1em; text-align: center; }
      .table td.tinytable_css_lc27ag893lie66w9vvqg, .table th.tinytable_css_lc27ag893lie66w9vvqg { text-align: left; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_ub49w7cfr2l66385lpb9" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead>
<tr>
<th scope="col" align="center" colspan="1" data-row="-1" data-col="0"> </th>
<th scope="col" align="center" colspan="2" data-row="-1" data-col="1">Biscoe</th>
<th scope="col" align="center" colspan="2" data-row="-1" data-col="2">Dream</th>
<th scope="col" align="center" colspan="2" data-row="-1" data-col="3">Torgersen</th>
</tr>
<tr>
<th scope="col" data-row="0" data-col="0"> </th>
                <th scope="col" data-row="0" data-col="1">Mean</th>
                <th scope="col" data-row="0" data-col="2">SD</th>
                <th scope="col" data-row="0" data-col="3">Mean</th>
                <th scope="col" data-row="0" data-col="4">SD</th>
                <th scope="col" data-row="0" data-col="5">Mean</th>
                <th scope="col" data-row="0" data-col="6">SD</th>
              </tr>
</thead>
<tbody>
<tr>
<td data-row="1" data-col="0">Flipper length (mm)</td>
                  <td data-row="1" data-col="1">209.71</td>
                  <td data-row="1" data-col="2">14.14</td>
                  <td data-row="1" data-col="3">193.07</td>
                  <td data-row="1" data-col="4">7.51</td>
                  <td data-row="1" data-col="5">191.20</td>
                  <td data-row="1" data-col="6">6.23</td>
                </tr>
<tr>
<td data-row="2" data-col="0">Body mass (g)</td>
                  <td data-row="2" data-col="1">4716.02</td>
                  <td data-row="2" data-col="2">782.86</td>
                  <td data-row="2" data-col="3">3712.90</td>
                  <td data-row="2" data-col="4">416.64</td>
                  <td data-row="2" data-col="5">3706.37</td>
                  <td data-row="2" data-col="6">445.11</td>
                </tr>
</tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
<p>The <code>Heading</code> function also has a <code>nearData</code> argument which can be useful in cases where some rows are nested but others are not. Compare the last row of these two tables:</p>
<div class="cell">
<div class="sourceCode" id="cb28"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">sex</span> <span class="op">*</span> <span class="op">(</span><span class="va">flipper_length_mm</span> <span class="op">+</span> <span class="va">bill_length_mm</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://dmurdoch.github.io/tables/reference/Heading.html">Heading</a></span><span class="op">(</span><span class="st">"Body mass (g)"</span><span class="op">)</span> <span class="op">*</span> <span class="va">body_mass_g</span> <span class="op">~</span> <span class="va">Mean</span> <span class="op">+</span> <span class="va">SD</span>,</span>
<span>            data <span class="op">=</span> <span class="va">penguins</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_4g53bpejqkv2h5lqvuq2(i, j, css_id) {
          var table = document.getElementById("tinytable_4g53bpejqkv2h5lqvuq2");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_4g53bpejqkv2h5lqvuq2(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_4g53bpejqkv2h5lqvuq2");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '5', j: 2 }, { i: '5', j: 3 },  ], css_id: 'tinytable_css_936luldknn4lyxbyly24',}, 
          { positions: [ { i: '1', j: 2 }, { i: '3', j: 2 }, { i: '4', j: 2 }, { i: '3', j: 3 }, { i: '2', j: 2 }, { i: '1', j: 3 }, { i: '2', j: 3 }, { i: '4', j: 3 },  ], css_id: 'tinytable_css_tj9r3fpnrf367z7q6b3k',}, 
          { positions: [ { i: '0', j: 2 }, { i: '0', j: 3 },  ], css_id: 'tinytable_css_vy66iqqtfqr7v9dhz4fj',}, 
          { positions: [ { i: '5', j: 0 }, { i: '5', j: 1 },  ], css_id: 'tinytable_css_k106dr99rutnp1vibcnm',}, 
          { positions: [ { i: '1', j: 0 }, { i: '3', j: 0 }, { i: '4', j: 0 }, { i: '3', j: 1 }, { i: '2', j: 0 }, { i: '1', j: 1 }, { i: '2', j: 1 }, { i: '4', j: 1 },  ], css_id: 'tinytable_css_db2ztmacw6hli33746wk',}, 
          { positions: [ { i: '0', j: 0 }, { i: '0', j: 1 },  ], css_id: 'tinytable_css_kfxvcllow5d994f58l49',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_4g53bpejqkv2h5lqvuq2(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_936luldknn4lyxbyly24, .table th.tinytable_css_936luldknn4lyxbyly24 { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_tj9r3fpnrf367z7q6b3k, .table th.tinytable_css_tj9r3fpnrf367z7q6b3k { text-align: right; }
      .table td.tinytable_css_vy66iqqtfqr7v9dhz4fj, .table th.tinytable_css_vy66iqqtfqr7v9dhz4fj { text-align: right; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_k106dr99rutnp1vibcnm, .table th.tinytable_css_k106dr99rutnp1vibcnm { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_db2ztmacw6hli33746wk, .table th.tinytable_css_db2ztmacw6hli33746wk { text-align: left; }
      .table td.tinytable_css_kfxvcllow5d994f58l49, .table th.tinytable_css_kfxvcllow5d994f58l49 { text-align: left; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_4g53bpejqkv2h5lqvuq2" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead><tr>
<th scope="col" data-row="0" data-col="0">sex</th>
                <th scope="col" data-row="0" data-col="1"> </th>
                <th scope="col" data-row="0" data-col="2">Mean</th>
                <th scope="col" data-row="0" data-col="3">SD</th>
              </tr></thead>
<tbody>
<tr>
<td data-row="1" data-col="0">female</td>
                  <td data-row="1" data-col="1">flipper_length_mm</td>
                  <td data-row="1" data-col="2">197.36</td>
                  <td data-row="1" data-col="3">12.50</td>
                </tr>
<tr>
<td data-row="2" data-col="0"></td>
                  <td data-row="2" data-col="1">bill_length_mm</td>
                  <td data-row="2" data-col="2">42.10</td>
                  <td data-row="2" data-col="3">4.90</td>
                </tr>
<tr>
<td data-row="3" data-col="0">male</td>
                  <td data-row="3" data-col="1">flipper_length_mm</td>
                  <td data-row="3" data-col="2">204.51</td>
                  <td data-row="3" data-col="3">14.55</td>
                </tr>
<tr>
<td data-row="4" data-col="0"></td>
                  <td data-row="4" data-col="1">bill_length_mm</td>
                  <td data-row="4" data-col="2">45.85</td>
                  <td data-row="4" data-col="3">5.37</td>
                </tr>
<tr>
<td data-row="5" data-col="0"></td>
                  <td data-row="5" data-col="1">Body mass (g)</td>
                  <td data-row="5" data-col="2">4201.75</td>
                  <td data-row="5" data-col="3">801.95</td>
                </tr>
</tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
<div class="sourceCode" id="cb29"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">sex</span> <span class="op">*</span> <span class="op">(</span><span class="va">flipper_length_mm</span> <span class="op">+</span> <span class="va">bill_length_mm</span><span class="op">)</span> <span class="op">+</span> <span class="fu"><a href="https://dmurdoch.github.io/tables/reference/Heading.html">Heading</a></span><span class="op">(</span><span class="st">"Body mass (g)"</span>, nearData<span class="op">=</span><span class="cn">FALSE</span><span class="op">)</span> <span class="op">*</span> <span class="va">body_mass_g</span> <span class="op">~</span> <span class="va">Mean</span> <span class="op">+</span> <span class="va">SD</span>,</span>
<span>            data <span class="op">=</span> <span class="va">penguins</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_64goggi5ct19b05m6jys(i, j, css_id) {
          var table = document.getElementById("tinytable_64goggi5ct19b05m6jys");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_64goggi5ct19b05m6jys(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_64goggi5ct19b05m6jys");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '5', j: 2 }, { i: '5', j: 3 },  ], css_id: 'tinytable_css_aziu6abbdidpmhstziyr',}, 
          { positions: [ { i: '1', j: 2 }, { i: '3', j: 2 }, { i: '4', j: 2 }, { i: '3', j: 3 }, { i: '2', j: 2 }, { i: '1', j: 3 }, { i: '2', j: 3 }, { i: '4', j: 3 },  ], css_id: 'tinytable_css_xyz2rpu47kx2a2hlxwy1',}, 
          { positions: [ { i: '0', j: 2 }, { i: '0', j: 3 },  ], css_id: 'tinytable_css_3b5muxbheey51ynjkeql',}, 
          { positions: [ { i: '5', j: 0 }, { i: '5', j: 1 },  ], css_id: 'tinytable_css_vb4qvh14net2rin9ti7g',}, 
          { positions: [ { i: '1', j: 0 }, { i: '3', j: 0 }, { i: '4', j: 0 }, { i: '3', j: 1 }, { i: '2', j: 0 }, { i: '1', j: 1 }, { i: '2', j: 1 }, { i: '4', j: 1 },  ], css_id: 'tinytable_css_jtcm74t3l2teth9t8drn',}, 
          { positions: [ { i: '0', j: 0 }, { i: '0', j: 1 },  ], css_id: 'tinytable_css_5z30vvuazfg9itaoj2n2',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_64goggi5ct19b05m6jys(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_aziu6abbdidpmhstziyr, .table th.tinytable_css_aziu6abbdidpmhstziyr { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_xyz2rpu47kx2a2hlxwy1, .table th.tinytable_css_xyz2rpu47kx2a2hlxwy1 { text-align: right; }
      .table td.tinytable_css_3b5muxbheey51ynjkeql, .table th.tinytable_css_3b5muxbheey51ynjkeql { text-align: right; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_vb4qvh14net2rin9ti7g, .table th.tinytable_css_vb4qvh14net2rin9ti7g { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_jtcm74t3l2teth9t8drn, .table th.tinytable_css_jtcm74t3l2teth9t8drn { text-align: left; }
      .table td.tinytable_css_5z30vvuazfg9itaoj2n2, .table th.tinytable_css_5z30vvuazfg9itaoj2n2 { text-align: left; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_64goggi5ct19b05m6jys" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead><tr>
<th scope="col" data-row="0" data-col="0">sex</th>
                <th scope="col" data-row="0" data-col="1"> </th>
                <th scope="col" data-row="0" data-col="2">Mean</th>
                <th scope="col" data-row="0" data-col="3">SD</th>
              </tr></thead>
<tbody>
<tr>
<td data-row="1" data-col="0">female</td>
                  <td data-row="1" data-col="1">flipper_length_mm</td>
                  <td data-row="1" data-col="2">197.36</td>
                  <td data-row="1" data-col="3">12.50</td>
                </tr>
<tr>
<td data-row="2" data-col="0"></td>
                  <td data-row="2" data-col="1">bill_length_mm</td>
                  <td data-row="2" data-col="2">42.10</td>
                  <td data-row="2" data-col="3">4.90</td>
                </tr>
<tr>
<td data-row="3" data-col="0">male</td>
                  <td data-row="3" data-col="1">flipper_length_mm</td>
                  <td data-row="3" data-col="2">204.51</td>
                  <td data-row="3" data-col="3">14.55</td>
                </tr>
<tr>
<td data-row="4" data-col="0"></td>
                  <td data-row="4" data-col="1">bill_length_mm</td>
                  <td data-row="4" data-col="2">45.85</td>
                  <td data-row="4" data-col="3">5.37</td>
                </tr>
<tr>
<td data-row="5" data-col="0">Body mass (g)</td>
                  <td data-row="5" data-col="1"></td>
                  <td data-row="5" data-col="2">4201.75</td>
                  <td data-row="5" data-col="3">801.95</td>
                </tr>
</tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
</section><section id="counts-and-percentages" class="level2"><h2 class="anchored" data-anchor-id="counts-and-percentages">Counts and Percentages</h2>
<p>The <code>tables</code> package allows <code>datasummary</code> to use neat tricks to produce frequency tables:</p>
<ol type="1">
<li>Add a <code>N</code> to the right-hand side of the equation.</li>
<li>Add <code><a href="https://dmurdoch.github.io/tables/reference/Percent.html">Percent()</a></code> to the right-hand side to calculate the percentage of observations in each cell.</li>
<li>Add <code>1</code> to the left-hand side to include a row with the total number of observations:</li>
</ol>
<div class="cell">
<div class="sourceCode" id="cb30"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">species</span> <span class="op">*</span> <span class="va">sex</span> <span class="op">+</span> <span class="fl">1</span> <span class="op">~</span> <span class="va">N</span> <span class="op">+</span> <span class="fu"><a href="https://dmurdoch.github.io/tables/reference/Percent.html">Percent</a></span><span class="op">(</span><span class="op">)</span>,</span>
<span>            data <span class="op">=</span> <span class="va">penguins</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_5dgqkgxmnptnyhregqgh(i, j, css_id) {
          var table = document.getElementById("tinytable_5dgqkgxmnptnyhregqgh");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_5dgqkgxmnptnyhregqgh(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_5dgqkgxmnptnyhregqgh");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '7', j: 2 }, { i: '7', j: 3 },  ], css_id: 'tinytable_css_jdqfax7lm74r856lx7vl',}, 
          { positions: [ { i: '1', j: 2 }, { i: '2', j: 2 }, { i: '3', j: 2 }, { i: '5', j: 2 }, { i: '6', j: 2 }, { i: '3', j: 3 }, { i: '4', j: 2 }, { i: '1', j: 3 }, { i: '2', j: 3 }, { i: '4', j: 3 }, { i: '5', j: 3 }, { i: '6', j: 3 },  ], css_id: 'tinytable_css_ax1q5uga2ibpehqz07iy',}, 
          { positions: [ { i: '0', j: 2 }, { i: '0', j: 3 },  ], css_id: 'tinytable_css_kb4gjpp2o5pyp989b7s8',}, 
          { positions: [ { i: '7', j: 0 }, { i: '7', j: 1 },  ], css_id: 'tinytable_css_rm2gi6p94imybjrkghet',}, 
          { positions: [ { i: '1', j: 0 }, { i: '2', j: 0 }, { i: '3', j: 0 }, { i: '5', j: 0 }, { i: '6', j: 0 }, { i: '3', j: 1 }, { i: '4', j: 0 }, { i: '1', j: 1 }, { i: '2', j: 1 }, { i: '4', j: 1 }, { i: '5', j: 1 }, { i: '6', j: 1 },  ], css_id: 'tinytable_css_me930swq44zploaqm3zr',}, 
          { positions: [ { i: '0', j: 0 }, { i: '0', j: 1 },  ], css_id: 'tinytable_css_gkpzvgqrqz2j11uobjy7',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_5dgqkgxmnptnyhregqgh(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_jdqfax7lm74r856lx7vl, .table th.tinytable_css_jdqfax7lm74r856lx7vl { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_ax1q5uga2ibpehqz07iy, .table th.tinytable_css_ax1q5uga2ibpehqz07iy { text-align: right; }
      .table td.tinytable_css_kb4gjpp2o5pyp989b7s8, .table th.tinytable_css_kb4gjpp2o5pyp989b7s8 { text-align: right; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_rm2gi6p94imybjrkghet, .table th.tinytable_css_rm2gi6p94imybjrkghet { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_me930swq44zploaqm3zr, .table th.tinytable_css_me930swq44zploaqm3zr { text-align: left; }
      .table td.tinytable_css_gkpzvgqrqz2j11uobjy7, .table th.tinytable_css_gkpzvgqrqz2j11uobjy7 { text-align: left; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_5dgqkgxmnptnyhregqgh" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead><tr>
<th scope="col" data-row="0" data-col="0">species</th>
                <th scope="col" data-row="0" data-col="1">sex</th>
                <th scope="col" data-row="0" data-col="2">N</th>
                <th scope="col" data-row="0" data-col="3">Percent</th>
              </tr></thead>
<tbody>
<tr>
<td data-row="1" data-col="0">Adelie</td>
                  <td data-row="1" data-col="1">female</td>
                  <td data-row="1" data-col="2">73</td>
                  <td data-row="1" data-col="3">21.22</td>
                </tr>
<tr>
<td data-row="2" data-col="0"></td>
                  <td data-row="2" data-col="1">male</td>
                  <td data-row="2" data-col="2">73</td>
                  <td data-row="2" data-col="3">21.22</td>
                </tr>
<tr>
<td data-row="3" data-col="0">Chinstrap</td>
                  <td data-row="3" data-col="1">female</td>
                  <td data-row="3" data-col="2">34</td>
                  <td data-row="3" data-col="3">9.88</td>
                </tr>
<tr>
<td data-row="4" data-col="0"></td>
                  <td data-row="4" data-col="1">male</td>
                  <td data-row="4" data-col="2">34</td>
                  <td data-row="4" data-col="3">9.88</td>
                </tr>
<tr>
<td data-row="5" data-col="0">Gentoo</td>
                  <td data-row="5" data-col="1">female</td>
                  <td data-row="5" data-col="2">58</td>
                  <td data-row="5" data-col="3">16.86</td>
                </tr>
<tr>
<td data-row="6" data-col="0"></td>
                  <td data-row="6" data-col="1">male</td>
                  <td data-row="6" data-col="2">61</td>
                  <td data-row="6" data-col="3">17.73</td>
                </tr>
<tr>
<td data-row="7" data-col="0"></td>
                  <td data-row="7" data-col="1">All</td>
                  <td data-row="7" data-col="2">344</td>
                  <td data-row="7" data-col="3">100.00</td>
                </tr>
</tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
<p>Note that the <code><a href="https://dmurdoch.github.io/tables/reference/Percent.html">Percent()</a></code> function accepts a <code>denom</code> argument to determine if percentages should be calculated row or column-wise, or if they should take into account all cells.</p>
</section><section id="weighted-percentages" class="level2"><h2 class="anchored" data-anchor-id="weighted-percentages">Weighted percentages</h2>
<p>The <code><a href="https://dmurdoch.github.io/tables/reference/Percent.html">Percent()</a></code> pseudo-function also accepts a <code>fn</code> argument, which must be a function which accepts two vectors: <code>x</code> is the values in the current cell, and <code>y</code> is all the values in the whole dataset. The default <code>fn</code> is:</p>
<div class="cell">
<div class="sourceCode" id="cb31"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">species</span> <span class="op">*</span> <span class="va">sex</span> <span class="op">+</span> <span class="fl">1</span> <span class="op">~</span> <span class="va">N</span> <span class="op">+</span> <span class="fu"><a href="https://dmurdoch.github.io/tables/reference/Percent.html">Percent</a></span><span class="op">(</span>fn <span class="op">=</span> <span class="kw">function</span><span class="op">(</span><span class="va">x</span>, <span class="va">y</span><span class="op">)</span> <span class="fl">100</span> <span class="op">*</span> <span class="fu"><a href="https://rdrr.io/r/base/length.html">length</a></span><span class="op">(</span><span class="va">x</span><span class="op">)</span> <span class="op">/</span> <span class="fu"><a href="https://rdrr.io/r/base/length.html">length</a></span><span class="op">(</span><span class="va">y</span><span class="op">)</span><span class="op">)</span>,</span>
<span>            data <span class="op">=</span> <span class="va">penguins</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_mao32bun9sa76m7bclgg(i, j, css_id) {
          var table = document.getElementById("tinytable_mao32bun9sa76m7bclgg");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_mao32bun9sa76m7bclgg(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_mao32bun9sa76m7bclgg");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '7', j: 2 }, { i: '7', j: 3 },  ], css_id: 'tinytable_css_rw7j8lgntm947lsr38g5',}, 
          { positions: [ { i: '1', j: 2 }, { i: '2', j: 2 }, { i: '3', j: 2 }, { i: '5', j: 2 }, { i: '6', j: 2 }, { i: '3', j: 3 }, { i: '4', j: 2 }, { i: '1', j: 3 }, { i: '2', j: 3 }, { i: '4', j: 3 }, { i: '5', j: 3 }, { i: '6', j: 3 },  ], css_id: 'tinytable_css_7riqto6h79r1m5m6znit',}, 
          { positions: [ { i: '0', j: 2 }, { i: '0', j: 3 },  ], css_id: 'tinytable_css_j83crms48tkbka1tro64',}, 
          { positions: [ { i: '7', j: 0 }, { i: '7', j: 1 },  ], css_id: 'tinytable_css_jc6s14tslmxo3iblz37f',}, 
          { positions: [ { i: '1', j: 0 }, { i: '2', j: 0 }, { i: '3', j: 0 }, { i: '5', j: 0 }, { i: '6', j: 0 }, { i: '3', j: 1 }, { i: '4', j: 0 }, { i: '1', j: 1 }, { i: '2', j: 1 }, { i: '4', j: 1 }, { i: '5', j: 1 }, { i: '6', j: 1 },  ], css_id: 'tinytable_css_b6n50ny9mq0hrsktw47s',}, 
          { positions: [ { i: '0', j: 0 }, { i: '0', j: 1 },  ], css_id: 'tinytable_css_q47jvoaotvxl5zj79zll',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_mao32bun9sa76m7bclgg(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_rw7j8lgntm947lsr38g5, .table th.tinytable_css_rw7j8lgntm947lsr38g5 { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_7riqto6h79r1m5m6znit, .table th.tinytable_css_7riqto6h79r1m5m6znit { text-align: right; }
      .table td.tinytable_css_j83crms48tkbka1tro64, .table th.tinytable_css_j83crms48tkbka1tro64 { text-align: right; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_jc6s14tslmxo3iblz37f, .table th.tinytable_css_jc6s14tslmxo3iblz37f { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_b6n50ny9mq0hrsktw47s, .table th.tinytable_css_b6n50ny9mq0hrsktw47s { text-align: left; }
      .table td.tinytable_css_q47jvoaotvxl5zj79zll, .table th.tinytable_css_q47jvoaotvxl5zj79zll { text-align: left; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_mao32bun9sa76m7bclgg" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead><tr>
<th scope="col" data-row="0" data-col="0">species</th>
                <th scope="col" data-row="0" data-col="1">sex</th>
                <th scope="col" data-row="0" data-col="2">N</th>
                <th scope="col" data-row="0" data-col="3">Percent</th>
              </tr></thead>
<tbody>
<tr>
<td data-row="1" data-col="0">Adelie</td>
                  <td data-row="1" data-col="1">female</td>
                  <td data-row="1" data-col="2">73</td>
                  <td data-row="1" data-col="3">21.22</td>
                </tr>
<tr>
<td data-row="2" data-col="0"></td>
                  <td data-row="2" data-col="1">male</td>
                  <td data-row="2" data-col="2">73</td>
                  <td data-row="2" data-col="3">21.22</td>
                </tr>
<tr>
<td data-row="3" data-col="0">Chinstrap</td>
                  <td data-row="3" data-col="1">female</td>
                  <td data-row="3" data-col="2">34</td>
                  <td data-row="3" data-col="3">9.88</td>
                </tr>
<tr>
<td data-row="4" data-col="0"></td>
                  <td data-row="4" data-col="1">male</td>
                  <td data-row="4" data-col="2">34</td>
                  <td data-row="4" data-col="3">9.88</td>
                </tr>
<tr>
<td data-row="5" data-col="0">Gentoo</td>
                  <td data-row="5" data-col="1">female</td>
                  <td data-row="5" data-col="2">58</td>
                  <td data-row="5" data-col="3">16.86</td>
                </tr>
<tr>
<td data-row="6" data-col="0"></td>
                  <td data-row="6" data-col="1">male</td>
                  <td data-row="6" data-col="2">61</td>
                  <td data-row="6" data-col="3">17.73</td>
                </tr>
<tr>
<td data-row="7" data-col="0"></td>
                  <td data-row="7" data-col="1">All</td>
                  <td data-row="7" data-col="2">344</td>
                  <td data-row="7" data-col="3">100.00</td>
                </tr>
</tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
<p>The code above takes the number of elements in the cell <code>length(x)</code> and divides it by the number of total elements <code>length(y)</code>.</p>
<p>Now, let’s say we want to display percentages weighted by one of the variables of the dataset. This can often be useful with survey weights, for example. Here, we use an arbitrary column of weights called <code>flipper_length_mm</code>:</p>
<div class="cell">
<div class="sourceCode" id="cb32"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va">wtpct</span> <span class="op">&lt;-</span> <span class="kw">function</span><span class="op">(</span><span class="va">x</span>, <span class="va">y</span><span class="op">)</span> <span class="fu"><a href="https://rdrr.io/r/base/sum.html">sum</a></span><span class="op">(</span><span class="va">x</span>, na.rm <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span> <span class="op">/</span> <span class="fu"><a href="https://rdrr.io/r/base/sum.html">sum</a></span><span class="op">(</span><span class="va">y</span>, na.rm <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span> <span class="op">*</span> <span class="fl">100</span></span>
<span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">species</span> <span class="op">*</span> <span class="va">sex</span> <span class="op">+</span> <span class="fl">1</span> <span class="op">~</span> <span class="va">N</span> <span class="op">+</span> <span class="va">flipper_length_mm</span> <span class="op">*</span> <span class="fu"><a href="https://dmurdoch.github.io/tables/reference/Percent.html">Percent</a></span><span class="op">(</span>fn <span class="op">=</span> <span class="va">wtpct</span><span class="op">)</span>,</span>
<span>            data <span class="op">=</span> <span class="va">penguins</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_oa36g26fyme829wmrbwv(i, j, css_id) {
          var table = document.getElementById("tinytable_oa36g26fyme829wmrbwv");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_oa36g26fyme829wmrbwv(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_oa36g26fyme829wmrbwv");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '7', j: 2 }, { i: '7', j: 3 },  ], css_id: 'tinytable_css_gmkhdcn3xtd4quxths1x',}, 
          { positions: [ { i: '1', j: 2 }, { i: '2', j: 2 }, { i: '3', j: 2 }, { i: '5', j: 2 }, { i: '6', j: 2 }, { i: '3', j: 3 }, { i: '4', j: 2 }, { i: '1', j: 3 }, { i: '2', j: 3 }, { i: '4', j: 3 }, { i: '5', j: 3 }, { i: '6', j: 3 },  ], css_id: 'tinytable_css_o00j1c0y8t1fpxeb35r8',}, 
          { positions: [ { i: '0', j: 2 }, { i: '0', j: 3 },  ], css_id: 'tinytable_css_zx8t1h9ab6xt0x1unjl8',}, 
          { positions: [ { i: '7', j: 0 }, { i: '7', j: 1 },  ], css_id: 'tinytable_css_z3offv51zfvvk8n4yzmw',}, 
          { positions: [ { i: '1', j: 0 }, { i: '2', j: 0 }, { i: '3', j: 0 }, { i: '5', j: 0 }, { i: '6', j: 0 }, { i: '3', j: 1 }, { i: '4', j: 0 }, { i: '1', j: 1 }, { i: '2', j: 1 }, { i: '4', j: 1 }, { i: '5', j: 1 }, { i: '6', j: 1 },  ], css_id: 'tinytable_css_jqafo6dlniw6cq6ks0ym',}, 
          { positions: [ { i: '0', j: 0 }, { i: '0', j: 1 },  ], css_id: 'tinytable_css_fvpvjze7uy56j081c683',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_oa36g26fyme829wmrbwv(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_gmkhdcn3xtd4quxths1x, .table th.tinytable_css_gmkhdcn3xtd4quxths1x { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_o00j1c0y8t1fpxeb35r8, .table th.tinytable_css_o00j1c0y8t1fpxeb35r8 { text-align: right; }
      .table td.tinytable_css_zx8t1h9ab6xt0x1unjl8, .table th.tinytable_css_zx8t1h9ab6xt0x1unjl8 { text-align: right; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_z3offv51zfvvk8n4yzmw, .table th.tinytable_css_z3offv51zfvvk8n4yzmw { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_jqafo6dlniw6cq6ks0ym, .table th.tinytable_css_jqafo6dlniw6cq6ks0ym { text-align: left; }
      .table td.tinytable_css_fvpvjze7uy56j081c683, .table th.tinytable_css_fvpvjze7uy56j081c683 { text-align: left; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_oa36g26fyme829wmrbwv" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead><tr>
<th scope="col" data-row="0" data-col="0">species</th>
                <th scope="col" data-row="0" data-col="1">sex</th>
                <th scope="col" data-row="0" data-col="2">N</th>
                <th scope="col" data-row="0" data-col="3">Percent</th>
              </tr></thead>
<tbody>
<tr>
<td data-row="1" data-col="0">Adelie</td>
                  <td data-row="1" data-col="1">female</td>
                  <td data-row="1" data-col="2">73</td>
                  <td data-row="1" data-col="3">19.95</td>
                </tr>
<tr>
<td data-row="2" data-col="0"></td>
                  <td data-row="2" data-col="1">male</td>
                  <td data-row="2" data-col="2">73</td>
                  <td data-row="2" data-col="3">20.44</td>
                </tr>
<tr>
<td data-row="3" data-col="0">Chinstrap</td>
                  <td data-row="3" data-col="1">female</td>
                  <td data-row="3" data-col="2">34</td>
                  <td data-row="3" data-col="3">9.49</td>
                </tr>
<tr>
<td data-row="4" data-col="0"></td>
                  <td data-row="4" data-col="1">male</td>
                  <td data-row="4" data-col="2">34</td>
                  <td data-row="4" data-col="3">9.89</td>
                </tr>
<tr>
<td data-row="5" data-col="0">Gentoo</td>
                  <td data-row="5" data-col="1">female</td>
                  <td data-row="5" data-col="2">58</td>
                  <td data-row="5" data-col="3">17.95</td>
                </tr>
<tr>
<td data-row="6" data-col="0"></td>
                  <td data-row="6" data-col="1">male</td>
                  <td data-row="6" data-col="2">61</td>
                  <td data-row="6" data-col="3">19.67</td>
                </tr>
<tr>
<td data-row="7" data-col="0"></td>
                  <td data-row="7" data-col="1">All</td>
                  <td data-row="7" data-col="2">344</td>
                  <td data-row="7" data-col="3">100.00</td>
                </tr>
</tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
<p>In each cell we now have the sum of weights in that cell, divided by the total sum of weights in the column.</p>
</section><section id="custom-percentages" class="level2"><h2 class="anchored" data-anchor-id="custom-percentages">Custom percentages</h2>
<p>Here is another simple illustration of Percent function mechanism in action, where we combine counts and percentages in a simple nice label:</p>
<div class="cell">
<div class="sourceCode" id="cb33"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va">dat</span> <span class="op">&lt;-</span> <span class="va">mtcars</span></span>
<span><span class="va">dat</span><span class="op">$</span><span class="va">cyl</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/factor.html">as.factor</a></span><span class="op">(</span><span class="va">dat</span><span class="op">$</span><span class="va">cyl</span><span class="op">)</span></span>
<span></span>
<span><span class="va">fn</span> <span class="op">&lt;-</span> <span class="kw">function</span><span class="op">(</span><span class="va">x</span>, <span class="va">y</span><span class="op">)</span> <span class="op">{</span></span>
<span>    <span class="va">out</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/sprintf.html">sprintf</a></span><span class="op">(</span></span>
<span>        <span class="st">"%s (%.1f%%)"</span>,</span>
<span>        <span class="fu"><a href="https://rdrr.io/r/base/length.html">length</a></span><span class="op">(</span><span class="va">x</span><span class="op">)</span>,</span>
<span>        <span class="fu"><a href="https://rdrr.io/r/base/length.html">length</a></span><span class="op">(</span><span class="va">x</span><span class="op">)</span> <span class="op">/</span> <span class="fu"><a href="https://rdrr.io/r/base/length.html">length</a></span><span class="op">(</span><span class="va">y</span><span class="op">)</span> <span class="op">*</span> <span class="fl">100</span><span class="op">)</span></span>
<span><span class="op">}</span></span>
<span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span></span>
<span>    <span class="va">cyl</span> <span class="op">~</span> <span class="fu"><a href="https://dmurdoch.github.io/tables/reference/Percent.html">Percent</a></span><span class="op">(</span>fn <span class="op">=</span> <span class="va">fn</span><span class="op">)</span>,</span>
<span>    data <span class="op">=</span> <span class="va">dat</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_ravgl5vxiucynriadj6y(i, j, css_id) {
          var table = document.getElementById("tinytable_ravgl5vxiucynriadj6y");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_ravgl5vxiucynriadj6y(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_ravgl5vxiucynriadj6y");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '3', j: 1 },  ], css_id: 'tinytable_css_fqfje1hpp9o4uwxxy2hp',}, 
          { positions: [ { i: '1', j: 1 }, { i: '2', j: 1 },  ], css_id: 'tinytable_css_vd354d0ienz23ffzveqr',}, 
          { positions: [ { i: '0', j: 1 },  ], css_id: 'tinytable_css_dqlcwcs04brl2344wh6a',}, 
          { positions: [ { i: '3', j: 0 },  ], css_id: 'tinytable_css_foiy02qqrc31nxx1sfd4',}, 
          { positions: [ { i: '1', j: 0 }, { i: '2', j: 0 },  ], css_id: 'tinytable_css_xikrcjhat2czhppe6adk',}, 
          { positions: [ { i: '0', j: 0 },  ], css_id: 'tinytable_css_ntlj6ux90rwfpf8xgx4l',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_ravgl5vxiucynriadj6y(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_fqfje1hpp9o4uwxxy2hp, .table th.tinytable_css_fqfje1hpp9o4uwxxy2hp { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_vd354d0ienz23ffzveqr, .table th.tinytable_css_vd354d0ienz23ffzveqr { text-align: right; }
      .table td.tinytable_css_dqlcwcs04brl2344wh6a, .table th.tinytable_css_dqlcwcs04brl2344wh6a { text-align: right; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_foiy02qqrc31nxx1sfd4, .table th.tinytable_css_foiy02qqrc31nxx1sfd4 { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_xikrcjhat2czhppe6adk, .table th.tinytable_css_xikrcjhat2czhppe6adk { text-align: left; }
      .table td.tinytable_css_ntlj6ux90rwfpf8xgx4l, .table th.tinytable_css_ntlj6ux90rwfpf8xgx4l { text-align: left; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_ravgl5vxiucynriadj6y" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead><tr>
<th scope="col" data-row="0" data-col="0">cyl</th>
                <th scope="col" data-row="0" data-col="1">Percent</th>
              </tr></thead>
<tbody>
<tr>
<td data-row="1" data-col="0">4</td>
                  <td data-row="1" data-col="1">11 (34.4%)</td>
                </tr>
<tr>
<td data-row="2" data-col="0">6</td>
                  <td data-row="2" data-col="1">7 (21.9%)</td>
                </tr>
<tr>
<td data-row="3" data-col="0">8</td>
                  <td data-row="3" data-col="1">14 (43.8%)</td>
                </tr>
</tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
</section><section id="factor" class="level2"><h2 class="anchored" data-anchor-id="factor"><code>Factor</code></h2>
<p>The <code>*</code> nesting operator that we used above works automatically for factor, character, and logical variables. Sometimes, it is convenient to use it with other types of variables, such as binary numeric variables. In that case, we can wrap the variable name inside a call to <code><a href="https://dmurdoch.github.io/tables/reference/RowFactor.html">Factor()</a></code>. This allows us to treat a variable as factor, without having to modify it in the original data. For example, in the <code>mtcars</code> data, there is a binary numeric variable call <code>am</code>. We nest statistics within categories of <code>am</code> by typing:</p>
<div class="cell">
<div class="sourceCode" id="cb34"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">mpg</span> <span class="op">+</span> <span class="va">hp</span> <span class="op">~</span> <span class="fu"><a href="https://dmurdoch.github.io/tables/reference/RowFactor.html">Factor</a></span><span class="op">(</span><span class="va">am</span><span class="op">)</span> <span class="op">*</span> <span class="op">(</span><span class="va">mean</span> <span class="op">+</span> <span class="va">sd</span><span class="op">)</span>,</span>
<span>            data <span class="op">=</span> <span class="va">mtcars</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_pwmithr25765x810dzl7(i, j, css_id) {
          var table = document.getElementById("tinytable_pwmithr25765x810dzl7");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_pwmithr25765x810dzl7(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_pwmithr25765x810dzl7");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '-1', j: 3 }, { i: '-1', j: 4 },  ], css_id: 'tinytable_css_buswhzh4okv1zh8zhcrw',}, 
          { positions: [ { i: '2', j: 1 }, { i: '2', j: 2 }, { i: '2', j: 3 }, { i: '2', j: 4 },  ], css_id: 'tinytable_css_k2ndgx96icyn6vweypbu',}, 
          { positions: [ { i: '1', j: 1 }, { i: '1', j: 2 }, { i: '1', j: 3 }, { i: '1', j: 4 },  ], css_id: 'tinytable_css_07qoca9x3wzxmqq4539i',}, 
          { positions: [ { i: '-1', j: 1 }, { i: '-1', j: 2 },  ], css_id: 'tinytable_css_ixc7huw11sb48doko2ic',}, 
          { positions: [ { i: '0', j: 1 }, { i: '0', j: 2 }, { i: '0', j: 3 }, { i: '0', j: 4 },  ], css_id: 'tinytable_css_4eruvbqg597k0h43dkyr',}, 
          { positions: [ { i: '2', j: 0 },  ], css_id: 'tinytable_css_q5hp6egxaxfckag212vx',}, 
          { positions: [ { i: '1', j: 0 },  ], css_id: 'tinytable_css_ofpb4d9owawe6vjapvw2',}, 
          { positions: [ { i: '-1', j: 0 },  ], css_id: 'tinytable_css_nnjrphmbzjvcyh7gv3ga',}, 
          { positions: [ { i: '0', j: 0 },  ], css_id: 'tinytable_css_9zsve1qr893eyqy7kztr',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_pwmithr25765x810dzl7(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_buswhzh4okv1zh8zhcrw, .table th.tinytable_css_buswhzh4okv1zh8zhcrw { text-align: right; text-align: center; border-top: solid #d3d8dc 0.1em; text-align: center; }
      .table td.tinytable_css_k2ndgx96icyn6vweypbu, .table th.tinytable_css_k2ndgx96icyn6vweypbu { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_07qoca9x3wzxmqq4539i, .table th.tinytable_css_07qoca9x3wzxmqq4539i { text-align: right; }
      .table td.tinytable_css_ixc7huw11sb48doko2ic, .table th.tinytable_css_ixc7huw11sb48doko2ic { text-align: right; text-align: center; border-top: solid #d3d8dc 0.1em; text-align: center; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_4eruvbqg597k0h43dkyr, .table th.tinytable_css_4eruvbqg597k0h43dkyr { text-align: right; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_q5hp6egxaxfckag212vx, .table th.tinytable_css_q5hp6egxaxfckag212vx { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_ofpb4d9owawe6vjapvw2, .table th.tinytable_css_ofpb4d9owawe6vjapvw2 { text-align: left; }
      .table td.tinytable_css_nnjrphmbzjvcyh7gv3ga, .table th.tinytable_css_nnjrphmbzjvcyh7gv3ga { text-align: left; text-align: center; border-top: solid #d3d8dc 0.1em; text-align: center; }
      .table td.tinytable_css_9zsve1qr893eyqy7kztr, .table th.tinytable_css_9zsve1qr893eyqy7kztr { text-align: left; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_pwmithr25765x810dzl7" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead>
<tr>
<th scope="col" align="center" colspan="1" data-row="-1" data-col="0"> </th>
<th scope="col" align="center" colspan="2" data-row="-1" data-col="1">0</th>
<th scope="col" align="center" colspan="2" data-row="-1" data-col="2">1</th>
</tr>
<tr>
<th scope="col" data-row="0" data-col="0"> </th>
                <th scope="col" data-row="0" data-col="1">mean</th>
                <th scope="col" data-row="0" data-col="2">sd</th>
                <th scope="col" data-row="0" data-col="3">mean</th>
                <th scope="col" data-row="0" data-col="4">sd</th>
              </tr>
</thead>
<tbody>
<tr>
<td data-row="1" data-col="0">mpg</td>
                  <td data-row="1" data-col="1">17.15</td>
                  <td data-row="1" data-col="2">3.83</td>
                  <td data-row="1" data-col="3">24.39</td>
                  <td data-row="1" data-col="4">6.17</td>
                </tr>
<tr>
<td data-row="2" data-col="0">hp</td>
                  <td data-row="2" data-col="1">160.26</td>
                  <td data-row="2" data-col="2">53.91</td>
                  <td data-row="2" data-col="3">126.85</td>
                  <td data-row="2" data-col="4">84.06</td>
                </tr>
</tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
</section><section id="arguments-na.rmtrue" class="level2"><h2 class="anchored" data-anchor-id="arguments-na.rmtrue">
<code>Arguments</code>: <code>na.rm=TRUE</code>
</h2>
<p>We can pass any argument to the summary function by including a call to <code><a href="https://dmurdoch.github.io/tables/reference/Arguments.html">Arguments()</a></code>. For instance, there are missing values in the <code>flipper_length_mm</code> variable of the <code>penguins</code> dataset. Therefore, the standard <code>mean</code> function will produce no result, because its default argument is <code>na.rm=FALSE</code>. We can change that by calling:</p>
<div class="cell">
<div class="sourceCode" id="cb35"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">flipper_length_mm</span> <span class="op">~</span> <span class="va">mean</span> <span class="op">+</span> <span class="va">mean</span><span class="op">*</span><span class="fu"><a href="https://dmurdoch.github.io/tables/reference/Arguments.html">Arguments</a></span><span class="op">(</span>na.rm<span class="op">=</span><span class="cn">TRUE</span><span class="op">)</span>,</span>
<span>            data <span class="op">=</span> <span class="va">penguins</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_kxqym11auotwnc97wjup(i, j, css_id) {
          var table = document.getElementById("tinytable_kxqym11auotwnc97wjup");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_kxqym11auotwnc97wjup(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_kxqym11auotwnc97wjup");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '1', j: 1 }, { i: '1', j: 2 },  ], css_id: 'tinytable_css_c20plgs0r92v476sy5yw',}, 
          { positions: [ { i: '0', j: 1 }, { i: '0', j: 2 },  ], css_id: 'tinytable_css_qiqbjgycuzooi41odvsd',}, 
          { positions: [ { i: '1', j: 0 },  ], css_id: 'tinytable_css_rkhefim4vtb6jsdycpk7',}, 
          { positions: [ { i: '0', j: 0 },  ], css_id: 'tinytable_css_b7nztlekkfjo94vuwy8o',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_kxqym11auotwnc97wjup(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_c20plgs0r92v476sy5yw, .table th.tinytable_css_c20plgs0r92v476sy5yw { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_qiqbjgycuzooi41odvsd, .table th.tinytable_css_qiqbjgycuzooi41odvsd { text-align: right; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_rkhefim4vtb6jsdycpk7, .table th.tinytable_css_rkhefim4vtb6jsdycpk7 { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_b7nztlekkfjo94vuwy8o, .table th.tinytable_css_b7nztlekkfjo94vuwy8o { text-align: left; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_kxqym11auotwnc97wjup" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead><tr>
<th scope="col" data-row="0" data-col="0"> </th>
                <th scope="col" data-row="0" data-col="1">mean</th>
                <th scope="col" data-row="0" data-col="2">mean </th>
              </tr></thead>
<tbody><tr>
<td data-row="1" data-col="0">flipper_length_mm</td>
                  <td data-row="1" data-col="1"></td>
                  <td data-row="1" data-col="2">200.92</td>
                </tr></tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
<p>Notice that there is an empty cell (<code>NA</code>) under the normal <code>mean</code> function, but that the <code>mean</code> call with <code>Arguments(na.rm=TRUE)</code> produced a numeric result.</p>
<p>We can pass the same arguments to multiple functions using the parentheses:</p>
<div class="cell">
<div class="sourceCode" id="cb36"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">flipper_length_mm</span> <span class="op">~</span> <span class="op">(</span><span class="va">mean</span> <span class="op">+</span> <span class="va">sd</span><span class="op">)</span> <span class="op">*</span> <span class="fu"><a href="https://dmurdoch.github.io/tables/reference/Arguments.html">Arguments</a></span><span class="op">(</span>na.rm<span class="op">=</span><span class="cn">TRUE</span><span class="op">)</span>,</span>
<span>            data <span class="op">=</span> <span class="va">penguins</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_6wnssbb82k3byldysat9(i, j, css_id) {
          var table = document.getElementById("tinytable_6wnssbb82k3byldysat9");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_6wnssbb82k3byldysat9(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_6wnssbb82k3byldysat9");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '1', j: 1 }, { i: '1', j: 2 },  ], css_id: 'tinytable_css_q44vejxbp8fkz6d6kmhr',}, 
          { positions: [ { i: '0', j: 1 }, { i: '0', j: 2 },  ], css_id: 'tinytable_css_hzvhrqzifbc0h44n1g15',}, 
          { positions: [ { i: '1', j: 0 },  ], css_id: 'tinytable_css_dfczz2996lq58zuj2t20',}, 
          { positions: [ { i: '0', j: 0 },  ], css_id: 'tinytable_css_50trtyv8qmgdgye2o63k',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_6wnssbb82k3byldysat9(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_q44vejxbp8fkz6d6kmhr, .table th.tinytable_css_q44vejxbp8fkz6d6kmhr { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_hzvhrqzifbc0h44n1g15, .table th.tinytable_css_hzvhrqzifbc0h44n1g15 { text-align: right; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_dfczz2996lq58zuj2t20, .table th.tinytable_css_dfczz2996lq58zuj2t20 { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_50trtyv8qmgdgye2o63k, .table th.tinytable_css_50trtyv8qmgdgye2o63k { text-align: left; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_6wnssbb82k3byldysat9" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead><tr>
<th scope="col" data-row="0" data-col="0"> </th>
                <th scope="col" data-row="0" data-col="1">mean</th>
                <th scope="col" data-row="0" data-col="2">sd</th>
              </tr></thead>
<tbody><tr>
<td data-row="1" data-col="0">flipper_length_mm</td>
                  <td data-row="1" data-col="1">200.92</td>
                  <td data-row="1" data-col="2">14.06</td>
                </tr></tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
<p>Note that the shortcut functions that <code>modelsummary</code> supplies use <code>na.rm=TRUE</code> by default, so we can use them directly without arguments, even when there are missing values:</p>
<div class="cell">
<div class="sourceCode" id="cb37"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">flipper_length_mm</span> <span class="op">~</span> <span class="va">Mean</span> <span class="op">+</span> <span class="va">Var</span> <span class="op">+</span> <span class="va">P75</span> <span class="op">+</span> <span class="va">Range</span>,</span>
<span>            data <span class="op">=</span> <span class="va">penguins</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_ltmo5mtcry5hm0cxjqic(i, j, css_id) {
          var table = document.getElementById("tinytable_ltmo5mtcry5hm0cxjqic");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_ltmo5mtcry5hm0cxjqic(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_ltmo5mtcry5hm0cxjqic");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '1', j: 1 }, { i: '1', j: 2 }, { i: '1', j: 3 }, { i: '1', j: 4 },  ], css_id: 'tinytable_css_73ej4hqgzzwbohtk3co8',}, 
          { positions: [ { i: '0', j: 1 }, { i: '0', j: 2 }, { i: '0', j: 3 }, { i: '0', j: 4 },  ], css_id: 'tinytable_css_ncf5qngp2a6mks7wuyva',}, 
          { positions: [ { i: '1', j: 0 },  ], css_id: 'tinytable_css_vxvweagotybq4g5ryyhl',}, 
          { positions: [ { i: '0', j: 0 },  ], css_id: 'tinytable_css_frpkyuzqzqh3nv25ewfw',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_ltmo5mtcry5hm0cxjqic(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_73ej4hqgzzwbohtk3co8, .table th.tinytable_css_73ej4hqgzzwbohtk3co8 { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_ncf5qngp2a6mks7wuyva, .table th.tinytable_css_ncf5qngp2a6mks7wuyva { text-align: right; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_vxvweagotybq4g5ryyhl, .table th.tinytable_css_vxvweagotybq4g5ryyhl { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_frpkyuzqzqh3nv25ewfw, .table th.tinytable_css_frpkyuzqzqh3nv25ewfw { text-align: left; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_ltmo5mtcry5hm0cxjqic" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead><tr>
<th scope="col" data-row="0" data-col="0"> </th>
                <th scope="col" data-row="0" data-col="1">Mean</th>
                <th scope="col" data-row="0" data-col="2">Var</th>
                <th scope="col" data-row="0" data-col="3">P75</th>
                <th scope="col" data-row="0" data-col="4">Range</th>
              </tr></thead>
<tbody><tr>
<td data-row="1" data-col="0">flipper_length_mm</td>
                  <td data-row="1" data-col="1">200.92</td>
                  <td data-row="1" data-col="2">197.73</td>
                  <td data-row="1" data-col="3">213.00</td>
                  <td data-row="1" data-col="4">59.00</td>
                </tr></tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
</section><section id="arguments-weighted-mean" class="level2"><h2 class="anchored" data-anchor-id="arguments-weighted-mean">
<code>Arguments</code>: Weighted Mean</h2>
<p>You can use the <code>Arguments</code> mechanism to do various things, such as calculating weighted means:</p>
<div class="cell">
<div class="sourceCode" id="cb38"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va">newdata</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html">data.frame</a></span><span class="op">(</span></span>
<span>  x <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/stats/Normal.html">rnorm</a></span><span class="op">(</span><span class="fl">20</span><span class="op">)</span>,</span>
<span>  w <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/stats/Normal.html">rnorm</a></span><span class="op">(</span><span class="fl">20</span><span class="op">)</span>,</span>
<span>  y <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/stats/Normal.html">rnorm</a></span><span class="op">(</span><span class="fl">20</span><span class="op">)</span><span class="op">)</span></span>
<span></span>
<span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">x</span> <span class="op">+</span> <span class="va">y</span> <span class="op">~</span> <span class="va">weighted.mean</span> <span class="op">*</span> <span class="fu"><a href="https://dmurdoch.github.io/tables/reference/Arguments.html">Arguments</a></span><span class="op">(</span>w <span class="op">=</span> <span class="va">w</span><span class="op">)</span>,</span>
<span>            data <span class="op">=</span> <span class="va">newdata</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_y9vn2gego7p9glki0do9(i, j, css_id) {
          var table = document.getElementById("tinytable_y9vn2gego7p9glki0do9");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_y9vn2gego7p9glki0do9(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_y9vn2gego7p9glki0do9");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '2', j: 1 },  ], css_id: 'tinytable_css_l44zn5pt9ecx0vb0zf12',}, 
          { positions: [ { i: '1', j: 1 },  ], css_id: 'tinytable_css_stkklir7kw5n0fcsvf4p',}, 
          { positions: [ { i: '0', j: 1 },  ], css_id: 'tinytable_css_v7tz50d4c70rt1yeobzm',}, 
          { positions: [ { i: '2', j: 0 },  ], css_id: 'tinytable_css_0dkh5xrywbii92h5vvz6',}, 
          { positions: [ { i: '1', j: 0 },  ], css_id: 'tinytable_css_l4t1pqdm5knuq4v39wml',}, 
          { positions: [ { i: '0', j: 0 },  ], css_id: 'tinytable_css_pbd1en23i7ej6aqp3yfx',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_y9vn2gego7p9glki0do9(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_l44zn5pt9ecx0vb0zf12, .table th.tinytable_css_l44zn5pt9ecx0vb0zf12 { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_stkklir7kw5n0fcsvf4p, .table th.tinytable_css_stkklir7kw5n0fcsvf4p { text-align: right; }
      .table td.tinytable_css_v7tz50d4c70rt1yeobzm, .table th.tinytable_css_v7tz50d4c70rt1yeobzm { text-align: right; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_0dkh5xrywbii92h5vvz6, .table th.tinytable_css_0dkh5xrywbii92h5vvz6 { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_l4t1pqdm5knuq4v39wml, .table th.tinytable_css_l4t1pqdm5knuq4v39wml { text-align: left; }
      .table td.tinytable_css_pbd1en23i7ej6aqp3yfx, .table th.tinytable_css_pbd1en23i7ej6aqp3yfx { text-align: left; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_y9vn2gego7p9glki0do9" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead><tr>
<th scope="col" data-row="0" data-col="0"> </th>
                <th scope="col" data-row="0" data-col="1">weighted.mean</th>
              </tr></thead>
<tbody>
<tr>
<td data-row="1" data-col="0">x</td>
                  <td data-row="1" data-col="1">-1.02</td>
                </tr>
<tr>
<td data-row="2" data-col="0">y</td>
                  <td data-row="2" data-col="1">-0.20</td>
                </tr>
</tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
<p>Which produces the same results as:</p>
<div class="cell">
<div class="sourceCode" id="cb39"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://rdrr.io/r/stats/weighted.mean.html">weighted.mean</a></span><span class="op">(</span><span class="va">newdata</span><span class="op">$</span><span class="va">x</span>, <span class="va">newdata</span><span class="op">$</span><span class="va">w</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] -1.024771</code></pre>
</div>
<div class="sourceCode" id="cb41"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://rdrr.io/r/stats/weighted.mean.html">weighted.mean</a></span><span class="op">(</span><span class="va">newdata</span><span class="op">$</span><span class="va">y</span>, <span class="va">newdata</span><span class="op">$</span><span class="va">w</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] -0.1993584</code></pre>
</div>
</div>
<p>But different results from:</p>
<div class="cell">
<div class="sourceCode" id="cb43"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><span class="op">(</span><span class="va">newdata</span><span class="op">$</span><span class="va">x</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] -0.5632302</code></pre>
</div>
<div class="sourceCode" id="cb45"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><span class="op">(</span><span class="va">newdata</span><span class="op">$</span><span class="va">y</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] -0.2580328</code></pre>
</div>
</div>
</section><section id="logical-subsets" class="level2"><h2 class="anchored" data-anchor-id="logical-subsets">Logical subsets</h2>
<p>Cool stuff is possible with logical subsets:</p>
<div class="cell">
<div class="sourceCode" id="cb47"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="op">(</span><span class="va">bill_length_mm</span> <span class="op">&gt;</span> <span class="fl">44.5</span><span class="op">)</span> <span class="op">+</span> <span class="op">(</span><span class="va">bill_length_mm</span> <span class="op">&lt;=</span> <span class="fl">44.5</span><span class="op">)</span> <span class="op">~</span> <span class="va">Mean</span> <span class="op">*</span> <span class="va">flipper_length_mm</span>,</span>
<span>            data <span class="op">=</span> <span class="va">penguins</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_eg4xj775fz4wl3iyeq6i(i, j, css_id) {
          var table = document.getElementById("tinytable_eg4xj775fz4wl3iyeq6i");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_eg4xj775fz4wl3iyeq6i(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_eg4xj775fz4wl3iyeq6i");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '2', j: 1 },  ], css_id: 'tinytable_css_j1eaxwfmy9z099e0d9t9',}, 
          { positions: [ { i: '1', j: 1 },  ], css_id: 'tinytable_css_nfessmik6u1rs55yhbcy',}, 
          { positions: [ { i: '0', j: 1 },  ], css_id: 'tinytable_css_7buk6699wwj61q55rimu',}, 
          { positions: [ { i: '2', j: 0 },  ], css_id: 'tinytable_css_xyykdnx62rkeqmpdxow0',}, 
          { positions: [ { i: '1', j: 0 },  ], css_id: 'tinytable_css_ombr3680cutbua253fni',}, 
          { positions: [ { i: '0', j: 0 },  ], css_id: 'tinytable_css_7xex519mzvks643lhhsx',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_eg4xj775fz4wl3iyeq6i(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_j1eaxwfmy9z099e0d9t9, .table th.tinytable_css_j1eaxwfmy9z099e0d9t9 { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_nfessmik6u1rs55yhbcy, .table th.tinytable_css_nfessmik6u1rs55yhbcy { text-align: right; }
      .table td.tinytable_css_7buk6699wwj61q55rimu, .table th.tinytable_css_7buk6699wwj61q55rimu { text-align: right; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_xyykdnx62rkeqmpdxow0, .table th.tinytable_css_xyykdnx62rkeqmpdxow0 { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_ombr3680cutbua253fni, .table th.tinytable_css_ombr3680cutbua253fni { text-align: left; }
      .table td.tinytable_css_7xex519mzvks643lhhsx, .table th.tinytable_css_7xex519mzvks643lhhsx { text-align: left; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_eg4xj775fz4wl3iyeq6i" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead><tr>
<th scope="col" data-row="0" data-col="0">NA</th>
                <th scope="col" data-row="0" data-col="1">NA</th>
              </tr></thead>
<tbody>
<tr>
<td data-row="1" data-col="0">bill_length_mm &gt; 44.5</td>
                  <td data-row="1" data-col="1">209.68</td>
                </tr>
<tr>
<td data-row="2" data-col="0">bill_length_mm 
                  </td>
<td data-row="2" data-col="1">192.45</td>
                </tr>
</tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
<p>See the <code>tables</code> package documentation for more details and examples.</p>
</section><section id="missing-values" class="level2"><h2 class="anchored" data-anchor-id="missing-values">Missing values</h2>
<p>At least 3 distinct issues can arise related to missing values.</p>
<section id="functions-and-na.rm" class="level3"><h3 class="anchored" data-anchor-id="functions-and-na.rm">Functions and <code>na.rm</code>
</h3>
<p>An empty cell can appear in the table when a statistical function returns <code>NA</code> instead of a numeric value. In those cases, you should:</p>
<ol type="1">
<li>Make sure that your statistical function (e.g., <code>mean</code> or <code>sd</code>) uses <code>na.rm=TRUE</code> by default</li>
<li>Use the <code>Arguments</code> strategy to set <code>na.rm=TRUE</code> (see the <code>Arguments</code> section of this vignette).</li>
<li>Use a convenience function supplied by <code>modelsummary</code>, where <code>na.rm</code> is <code>TRUE</code> by default: <code>Mean</code>, <code>SD</code>, <code>P25</code>, etc.</li>
</ol></section><section id="empty-crosstab-cells" class="level3"><h3 class="anchored" data-anchor-id="empty-crosstab-cells">Empty crosstab cells</h3>
<p>An empty cell can appear in the table when a crosstab is deeply nested, and there are no observations for a given combination of covariates. In those cases, you can use the <code>* DropEmpty</code> pseudo-function. See the “Empty cells” section of this vignette for examples.</p>
<div class="cell">
<div class="sourceCode" id="cb48"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">island</span> <span class="op">*</span> <span class="va">species</span> <span class="op">*</span> <span class="va">body_mass_g</span> <span class="op">~</span> <span class="va">sex</span> <span class="op">*</span> <span class="op">(</span><span class="va">Mean</span> <span class="op">+</span> <span class="va">SD</span><span class="op">)</span>,</span>
<span>            data <span class="op">=</span> <span class="va">penguins</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_9yofi1832d8z3tw2eqxr(i, j, css_id) {
          var table = document.getElementById("tinytable_9yofi1832d8z3tw2eqxr");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_9yofi1832d8z3tw2eqxr(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_9yofi1832d8z3tw2eqxr");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '-1', j: 5 }, { i: '-1', j: 6 },  ], css_id: 'tinytable_css_2fp2xiwth1hz891rwgd3',}, 
          { positions: [ { i: '9', j: 5 }, { i: '9', j: 3 }, { i: '9', j: 4 }, { i: '9', j: 6 },  ], css_id: 'tinytable_css_lyb78f0rycfxdonxyq7s',}, 
          { positions: [ { i: '1', j: 3 }, { i: '5', j: 3 }, { i: '3', j: 3 }, { i: '4', j: 3 }, { i: '2', j: 6 }, { i: '2', j: 3 }, { i: '7', j: 3 }, { i: '1', j: 4 }, { i: '2', j: 4 }, { i: '3', j: 4 }, { i: '4', j: 4 }, { i: '5', j: 4 }, { i: '6', j: 4 }, { i: '7', j: 4 }, { i: '6', j: 3 }, { i: '2', j: 5 }, { i: '8', j: 3 }, { i: '4', j: 5 }, { i: '1', j: 5 }, { i: '6', j: 5 }, { i: '3', j: 5 }, { i: '8', j: 5 }, { i: '5', j: 5 }, { i: '8', j: 4 }, { i: '7', j: 5 }, { i: '1', j: 6 }, { i: '6', j: 6 }, { i: '3', j: 6 }, { i: '4', j: 6 }, { i: '5', j: 6 }, { i: '7', j: 6 }, { i: '8', j: 6 },  ], css_id: 'tinytable_css_ckqw1mmv6b1fpgmfi8zz',}, 
          { positions: [ { i: '-1', j: 4 }, { i: '-1', j: 3 },  ], css_id: 'tinytable_css_fms207ohyl3cebm126ru',}, 
          { positions: [ { i: '0', j: 3 }, { i: '0', j: 5 }, { i: '0', j: 6 }, { i: '0', j: 4 },  ], css_id: 'tinytable_css_v3yhuw3w9otd55xl2pqq',}, 
          { positions: [ { i: '9', j: 1 }, { i: '9', j: 0 }, { i: '9', j: 2 },  ], css_id: 'tinytable_css_ltm4tlvfz9283c8dp4c3',}, 
          { positions: [ { i: '1', j: 0 }, { i: '2', j: 0 }, { i: '3', j: 0 }, { i: '4', j: 0 }, { i: '5', j: 0 }, { i: '6', j: 0 }, { i: '7', j: 0 }, { i: '1', j: 1 }, { i: '2', j: 1 }, { i: '3', j: 1 }, { i: '4', j: 1 }, { i: '5', j: 1 }, { i: '6', j: 1 }, { i: '7', j: 1 }, { i: '8', j: 1 }, { i: '2', j: 2 }, { i: '8', j: 0 }, { i: '4', j: 2 }, { i: '1', j: 2 }, { i: '6', j: 2 }, { i: '3', j: 2 }, { i: '8', j: 2 }, { i: '5', j: 2 }, { i: '7', j: 2 },  ], css_id: 'tinytable_css_l6197k9xvh6kwxwh53zm',}, 
          { positions: [ { i: '-1', j: 2 }, { i: '-1', j: 1 }, { i: '-1', j: 0 },  ], css_id: 'tinytable_css_jfmikq8plk135hbg2bbp',}, 
          { positions: [ { i: '0', j: 0 }, { i: '0', j: 2 }, { i: '0', j: 1 },  ], css_id: 'tinytable_css_cfv9vmhoem7oo5gvayai',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_9yofi1832d8z3tw2eqxr(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_2fp2xiwth1hz891rwgd3, .table th.tinytable_css_2fp2xiwth1hz891rwgd3 { text-align: right; text-align: center; border-top: solid #d3d8dc 0.1em; text-align: center; }
      .table td.tinytable_css_lyb78f0rycfxdonxyq7s, .table th.tinytable_css_lyb78f0rycfxdonxyq7s { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_ckqw1mmv6b1fpgmfi8zz, .table th.tinytable_css_ckqw1mmv6b1fpgmfi8zz { text-align: right; }
      .table td.tinytable_css_fms207ohyl3cebm126ru, .table th.tinytable_css_fms207ohyl3cebm126ru { text-align: right; text-align: center; border-top: solid #d3d8dc 0.1em; text-align: center; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_v3yhuw3w9otd55xl2pqq, .table th.tinytable_css_v3yhuw3w9otd55xl2pqq { text-align: right; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_ltm4tlvfz9283c8dp4c3, .table th.tinytable_css_ltm4tlvfz9283c8dp4c3 { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_l6197k9xvh6kwxwh53zm, .table th.tinytable_css_l6197k9xvh6kwxwh53zm { text-align: left; }
      .table td.tinytable_css_jfmikq8plk135hbg2bbp, .table th.tinytable_css_jfmikq8plk135hbg2bbp { text-align: left; text-align: center; border-top: solid #d3d8dc 0.1em; text-align: center; }
      .table td.tinytable_css_cfv9vmhoem7oo5gvayai, .table th.tinytable_css_cfv9vmhoem7oo5gvayai { text-align: left; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_9yofi1832d8z3tw2eqxr" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead>
<tr>
<th scope="col" align="center" colspan="1" data-row="-1" data-col="0"> </th>
<th scope="col" align="center" colspan="1" data-row="-1" data-col="1"> </th>
<th scope="col" align="center" colspan="1" data-row="-1" data-col="2"> </th>
<th scope="col" align="center" colspan="2" data-row="-1" data-col="3">female</th>
<th scope="col" align="center" colspan="2" data-row="-1" data-col="4">male</th>
</tr>
<tr>
<th scope="col" data-row="0" data-col="0">island</th>
                <th scope="col" data-row="0" data-col="1">species</th>
                <th scope="col" data-row="0" data-col="2"> </th>
                <th scope="col" data-row="0" data-col="3">Mean</th>
                <th scope="col" data-row="0" data-col="4">SD</th>
                <th scope="col" data-row="0" data-col="5">Mean</th>
                <th scope="col" data-row="0" data-col="6">SD</th>
              </tr>
</thead>
<tbody>
<tr>
<td data-row="1" data-col="0">Biscoe</td>
                  <td data-row="1" data-col="1">Adelie</td>
                  <td data-row="1" data-col="2">body_mass_g</td>
                  <td data-row="1" data-col="3">3369.32</td>
                  <td data-row="1" data-col="4">343.47</td>
                  <td data-row="1" data-col="5">4050.00</td>
                  <td data-row="1" data-col="6">355.57</td>
                </tr>
<tr>
<td data-row="2" data-col="0"></td>
                  <td data-row="2" data-col="1">Chinstrap</td>
                  <td data-row="2" data-col="2">body_mass_g</td>
                  <td data-row="2" data-col="3"></td>
                  <td data-row="2" data-col="4"></td>
                  <td data-row="2" data-col="5"></td>
                  <td data-row="2" data-col="6"></td>
                </tr>
<tr>
<td data-row="3" data-col="0"></td>
                  <td data-row="3" data-col="1">Gentoo</td>
                  <td data-row="3" data-col="2">body_mass_g</td>
                  <td data-row="3" data-col="3">4679.74</td>
                  <td data-row="3" data-col="4">281.58</td>
                  <td data-row="3" data-col="5">5484.84</td>
                  <td data-row="3" data-col="6">313.16</td>
                </tr>
<tr>
<td data-row="4" data-col="0">Dream</td>
                  <td data-row="4" data-col="1">Adelie</td>
                  <td data-row="4" data-col="2">body_mass_g</td>
                  <td data-row="4" data-col="3">3344.44</td>
                  <td data-row="4" data-col="4">212.06</td>
                  <td data-row="4" data-col="5">4045.54</td>
                  <td data-row="4" data-col="6">330.55</td>
                </tr>
<tr>
<td data-row="5" data-col="0"></td>
                  <td data-row="5" data-col="1">Chinstrap</td>
                  <td data-row="5" data-col="2">body_mass_g</td>
                  <td data-row="5" data-col="3">3527.21</td>
                  <td data-row="5" data-col="4">285.33</td>
                  <td data-row="5" data-col="5">3938.97</td>
                  <td data-row="5" data-col="6">362.14</td>
                </tr>
<tr>
<td data-row="6" data-col="0"></td>
                  <td data-row="6" data-col="1">Gentoo</td>
                  <td data-row="6" data-col="2">body_mass_g</td>
                  <td data-row="6" data-col="3"></td>
                  <td data-row="6" data-col="4"></td>
                  <td data-row="6" data-col="5"></td>
                  <td data-row="6" data-col="6"></td>
                </tr>
<tr>
<td data-row="7" data-col="0">Torgersen</td>
                  <td data-row="7" data-col="1">Adelie</td>
                  <td data-row="7" data-col="2">body_mass_g</td>
                  <td data-row="7" data-col="3">3395.83</td>
                  <td data-row="7" data-col="4">259.14</td>
                  <td data-row="7" data-col="5">4034.78</td>
                  <td data-row="7" data-col="6">372.47</td>
                </tr>
<tr>
<td data-row="8" data-col="0"></td>
                  <td data-row="8" data-col="1">Chinstrap</td>
                  <td data-row="8" data-col="2">body_mass_g</td>
                  <td data-row="8" data-col="3"></td>
                  <td data-row="8" data-col="4"></td>
                  <td data-row="8" data-col="5"></td>
                  <td data-row="8" data-col="6"></td>
                </tr>
<tr>
<td data-row="9" data-col="0"></td>
                  <td data-row="9" data-col="1">Gentoo</td>
                  <td data-row="9" data-col="2">body_mass_g</td>
                  <td data-row="9" data-col="3"></td>
                  <td data-row="9" data-col="4"></td>
                  <td data-row="9" data-col="5"></td>
                  <td data-row="9" data-col="6"></td>
                </tr>
</tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
<div class="sourceCode" id="cb49"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">island</span> <span class="op">*</span> <span class="va">species</span> <span class="op">*</span> <span class="va">body_mass_g</span> <span class="op">~</span> <span class="va">sex</span> <span class="op">*</span> <span class="op">(</span><span class="va">Mean</span> <span class="op">+</span> <span class="va">SD</span><span class="op">)</span> <span class="op">*</span> <span class="fu"><a href="https://dmurdoch.github.io/tables/reference/DropEmpty.html">DropEmpty</a></span><span class="op">(</span><span class="op">)</span>,</span>
<span>            data <span class="op">=</span> <span class="va">penguins</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_v0wyzo8d4ua43p2s7k4h(i, j, css_id) {
          var table = document.getElementById("tinytable_v0wyzo8d4ua43p2s7k4h");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_v0wyzo8d4ua43p2s7k4h(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_v0wyzo8d4ua43p2s7k4h");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '-1', j: 6 }, { i: '-1', j: 5 },  ], css_id: 'tinytable_css_lnkha6tf1eh8yfvvbrj5',}, 
          { positions: [ { i: '5', j: 3 }, { i: '5', j: 4 }, { i: '5', j: 6 }, { i: '5', j: 5 },  ], css_id: 'tinytable_css_d0n1ijya4b6svqzv00i8',}, 
          { positions: [ { i: '1', j: 3 }, { i: '2', j: 3 }, { i: '3', j: 3 }, { i: '4', j: 3 }, { i: '4', j: 5 }, { i: '3', j: 4 }, { i: '4', j: 4 }, { i: '1', j: 4 }, { i: '2', j: 4 }, { i: '1', j: 6 }, { i: '1', j: 5 }, { i: '4', j: 6 }, { i: '3', j: 5 }, { i: '2', j: 6 }, { i: '2', j: 5 }, { i: '3', j: 6 },  ], css_id: 'tinytable_css_xtag4vyslqhc9nla1t99',}, 
          { positions: [ { i: '-1', j: 3 }, { i: '-1', j: 4 },  ], css_id: 'tinytable_css_7l1pj886dlhx0ajh74sx',}, 
          { positions: [ { i: '0', j: 3 }, { i: '0', j: 5 }, { i: '0', j: 4 }, { i: '0', j: 6 },  ], css_id: 'tinytable_css_viw7c16juu6msphqtlze',}, 
          { positions: [ { i: '5', j: 1 }, { i: '5', j: 0 }, { i: '5', j: 2 },  ], css_id: 'tinytable_css_2ll906h2fsn1tuejru31',}, 
          { positions: [ { i: '4', j: 0 }, { i: '2', j: 0 }, { i: '3', j: 0 }, { i: '1', j: 1 }, { i: '4', j: 2 }, { i: '3', j: 1 }, { i: '4', j: 1 }, { i: '2', j: 2 }, { i: '2', j: 1 }, { i: '1', j: 0 }, { i: '1', j: 2 }, { i: '3', j: 2 },  ], css_id: 'tinytable_css_iohybfjltnhz44lmtbq7',}, 
          { positions: [ { i: '-1', j: 2 }, { i: '-1', j: 1 }, { i: '-1', j: 0 },  ], css_id: 'tinytable_css_gyjks73dlq68ndke8y5w',}, 
          { positions: [ { i: '0', j: 0 }, { i: '0', j: 2 }, { i: '0', j: 1 },  ], css_id: 'tinytable_css_87gl4x4uya155fgbnmc0',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_v0wyzo8d4ua43p2s7k4h(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_lnkha6tf1eh8yfvvbrj5, .table th.tinytable_css_lnkha6tf1eh8yfvvbrj5 { text-align: right; text-align: center; border-top: solid #d3d8dc 0.1em; text-align: center; }
      .table td.tinytable_css_d0n1ijya4b6svqzv00i8, .table th.tinytable_css_d0n1ijya4b6svqzv00i8 { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_xtag4vyslqhc9nla1t99, .table th.tinytable_css_xtag4vyslqhc9nla1t99 { text-align: right; }
      .table td.tinytable_css_7l1pj886dlhx0ajh74sx, .table th.tinytable_css_7l1pj886dlhx0ajh74sx { text-align: right; text-align: center; border-top: solid #d3d8dc 0.1em; text-align: center; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_viw7c16juu6msphqtlze, .table th.tinytable_css_viw7c16juu6msphqtlze { text-align: right; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_2ll906h2fsn1tuejru31, .table th.tinytable_css_2ll906h2fsn1tuejru31 { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_iohybfjltnhz44lmtbq7, .table th.tinytable_css_iohybfjltnhz44lmtbq7 { text-align: left; }
      .table td.tinytable_css_gyjks73dlq68ndke8y5w, .table th.tinytable_css_gyjks73dlq68ndke8y5w { text-align: left; text-align: center; border-top: solid #d3d8dc 0.1em; text-align: center; }
      .table td.tinytable_css_87gl4x4uya155fgbnmc0, .table th.tinytable_css_87gl4x4uya155fgbnmc0 { text-align: left; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_v0wyzo8d4ua43p2s7k4h" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead>
<tr>
<th scope="col" align="center" colspan="1" data-row="-1" data-col="0"> </th>
<th scope="col" align="center" colspan="1" data-row="-1" data-col="1"> </th>
<th scope="col" align="center" colspan="1" data-row="-1" data-col="2"> </th>
<th scope="col" align="center" colspan="2" data-row="-1" data-col="3">female</th>
<th scope="col" align="center" colspan="2" data-row="-1" data-col="4">male</th>
</tr>
<tr>
<th scope="col" data-row="0" data-col="0">island</th>
                <th scope="col" data-row="0" data-col="1">species</th>
                <th scope="col" data-row="0" data-col="2"> </th>
                <th scope="col" data-row="0" data-col="3">Mean</th>
                <th scope="col" data-row="0" data-col="4">SD</th>
                <th scope="col" data-row="0" data-col="5">Mean</th>
                <th scope="col" data-row="0" data-col="6">SD</th>
              </tr>
</thead>
<tbody>
<tr>
<td data-row="1" data-col="0">Biscoe</td>
                  <td data-row="1" data-col="1">Adelie</td>
                  <td data-row="1" data-col="2">body_mass_g</td>
                  <td data-row="1" data-col="3">3369.32</td>
                  <td data-row="1" data-col="4">343.47</td>
                  <td data-row="1" data-col="5">4050.00</td>
                  <td data-row="1" data-col="6">355.57</td>
                </tr>
<tr>
<td data-row="2" data-col="0"></td>
                  <td data-row="2" data-col="1">Gentoo</td>
                  <td data-row="2" data-col="2">body_mass_g</td>
                  <td data-row="2" data-col="3">4679.74</td>
                  <td data-row="2" data-col="4">281.58</td>
                  <td data-row="2" data-col="5">5484.84</td>
                  <td data-row="2" data-col="6">313.16</td>
                </tr>
<tr>
<td data-row="3" data-col="0">Dream</td>
                  <td data-row="3" data-col="1">Adelie</td>
                  <td data-row="3" data-col="2">body_mass_g</td>
                  <td data-row="3" data-col="3">3344.44</td>
                  <td data-row="3" data-col="4">212.06</td>
                  <td data-row="3" data-col="5">4045.54</td>
                  <td data-row="3" data-col="6">330.55</td>
                </tr>
<tr>
<td data-row="4" data-col="0"></td>
                  <td data-row="4" data-col="1">Chinstrap</td>
                  <td data-row="4" data-col="2">body_mass_g</td>
                  <td data-row="4" data-col="3">3527.21</td>
                  <td data-row="4" data-col="4">285.33</td>
                  <td data-row="4" data-col="5">3938.97</td>
                  <td data-row="4" data-col="6">362.14</td>
                </tr>
<tr>
<td data-row="5" data-col="0">Torgersen</td>
                  <td data-row="5" data-col="1">Adelie</td>
                  <td data-row="5" data-col="2">body_mass_g</td>
                  <td data-row="5" data-col="3">3395.83</td>
                  <td data-row="5" data-col="4">259.14</td>
                  <td data-row="5" data-col="5">4034.78</td>
                  <td data-row="5" data-col="6">372.47</td>
                </tr>
</tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
</section><section id="percent" class="level3"><h3 class="anchored" data-anchor-id="percent">Percent()</h3>
<p>By default, the <code><a href="https://dmurdoch.github.io/tables/reference/Percent.html">Percent()</a></code> function computes the share of each category with respect to the whole data, including missing observations, but we can work around this with some trickery:jig</p>
<div class="cell">
<div class="sourceCode" id="cb50"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va">dat</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html">data.frame</a></span><span class="op">(</span>z <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">0</span>,<span class="fl">0</span>,<span class="fl">1</span>,<span class="fl">0</span>,<span class="cn">NA</span>,<span class="fl">0</span>,<span class="fl">1</span><span class="op">)</span><span class="op">)</span></span>
<span></span>
<span><span class="fu"><a href="https://rdrr.io/r/base/proportions.html">prop.table</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/table.html">table</a></span><span class="op">(</span><span class="va">dat</span><span class="op">$</span><span class="va">z</span><span class="op">)</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>
        0         1 
0.6666667 0.3333333 </code></pre>
</div>
<div class="sourceCode" id="cb52"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="fu"><a href="https://dmurdoch.github.io/tables/reference/RowFactor.html">Factor</a></span><span class="op">(</span><span class="va">z</span><span class="op">)</span> <span class="op">~</span> <span class="va">N</span> <span class="op">+</span> <span class="fu"><a href="https://dmurdoch.github.io/tables/reference/Percent.html">Percent</a></span><span class="op">(</span><span class="op">)</span>, data <span class="op">=</span> <span class="va">dat</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_5o1g0yu3q7ci2x7hwk79(i, j, css_id) {
          var table = document.getElementById("tinytable_5o1g0yu3q7ci2x7hwk79");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_5o1g0yu3q7ci2x7hwk79(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_5o1g0yu3q7ci2x7hwk79");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '2', j: 1 }, { i: '2', j: 2 },  ], css_id: 'tinytable_css_ffg79vag4d8av9k2tavz',}, 
          { positions: [ { i: '1', j: 1 }, { i: '1', j: 2 },  ], css_id: 'tinytable_css_n31kmgvlzps93032hgri',}, 
          { positions: [ { i: '0', j: 1 }, { i: '0', j: 2 },  ], css_id: 'tinytable_css_4d6eimpswlajls60f7ui',}, 
          { positions: [ { i: '2', j: 0 },  ], css_id: 'tinytable_css_xn8enw8ytptmxhfeqoc2',}, 
          { positions: [ { i: '1', j: 0 },  ], css_id: 'tinytable_css_0ffw0jqhaavurrtg6q43',}, 
          { positions: [ { i: '0', j: 0 },  ], css_id: 'tinytable_css_gnvxd0xin69omy82f66i',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_5o1g0yu3q7ci2x7hwk79(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_ffg79vag4d8av9k2tavz, .table th.tinytable_css_ffg79vag4d8av9k2tavz { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_n31kmgvlzps93032hgri, .table th.tinytable_css_n31kmgvlzps93032hgri { text-align: right; }
      .table td.tinytable_css_4d6eimpswlajls60f7ui, .table th.tinytable_css_4d6eimpswlajls60f7ui { text-align: right; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_xn8enw8ytptmxhfeqoc2, .table th.tinytable_css_xn8enw8ytptmxhfeqoc2 { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_0ffw0jqhaavurrtg6q43, .table th.tinytable_css_0ffw0jqhaavurrtg6q43 { text-align: left; }
      .table td.tinytable_css_gnvxd0xin69omy82f66i, .table th.tinytable_css_gnvxd0xin69omy82f66i { text-align: left; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_5o1g0yu3q7ci2x7hwk79" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead><tr>
<th scope="col" data-row="0" data-col="0">z</th>
                <th scope="col" data-row="0" data-col="1">N</th>
                <th scope="col" data-row="0" data-col="2">Percent</th>
              </tr></thead>
<tbody>
<tr>
<td data-row="1" data-col="0">0</td>
                  <td data-row="1" data-col="1">4</td>
                  <td data-row="1" data-col="2">57.14</td>
                </tr>
<tr>
<td data-row="2" data-col="0">1</td>
                  <td data-row="2" data-col="1">2</td>
                  <td data-row="2" data-col="2">28.57</td>
                </tr>
</tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
<p>To count percentages without missing values, we can exploit the fact that <code><a href="https://dmurdoch.github.io/tables/reference/Percent.html">Percent()</a></code> accepts a <code>denom</code> argument which can be a logical vector:</p>
<div class="cell">
<div class="sourceCode" id="cb53"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="fu"><a href="https://dmurdoch.github.io/tables/reference/RowFactor.html">Factor</a></span><span class="op">(</span><span class="va">z</span><span class="op">)</span> <span class="op">~</span> <span class="va">N</span> <span class="op">+</span> <span class="fu"><a href="https://dmurdoch.github.io/tables/reference/Percent.html">Percent</a></span><span class="op">(</span>denom <span class="op">=</span> <span class="op">!</span><span class="fu"><a href="https://rdrr.io/r/base/NA.html">is.na</a></span><span class="op">(</span><span class="va">dat</span><span class="op">$</span><span class="va">z</span><span class="op">)</span><span class="op">)</span>, data <span class="op">=</span> <span class="va">dat</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_81ulk59x9efol9oeq5kj(i, j, css_id) {
          var table = document.getElementById("tinytable_81ulk59x9efol9oeq5kj");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_81ulk59x9efol9oeq5kj(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_81ulk59x9efol9oeq5kj");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '2', j: 1 }, { i: '2', j: 2 },  ], css_id: 'tinytable_css_5tvli6xsyv4whlrqbrr9',}, 
          { positions: [ { i: '1', j: 1 }, { i: '1', j: 2 },  ], css_id: 'tinytable_css_lsicsdfkx5d3j2mqoqhj',}, 
          { positions: [ { i: '0', j: 1 }, { i: '0', j: 2 },  ], css_id: 'tinytable_css_17zp1pn50jm9oxcbhtn9',}, 
          { positions: [ { i: '2', j: 0 },  ], css_id: 'tinytable_css_ycl1e1kbbuo9cjgh0xp3',}, 
          { positions: [ { i: '1', j: 0 },  ], css_id: 'tinytable_css_15ws2awlvk1mb2xvmf0c',}, 
          { positions: [ { i: '0', j: 0 },  ], css_id: 'tinytable_css_y22e3n01ibs1ctf0wnkr',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_81ulk59x9efol9oeq5kj(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_5tvli6xsyv4whlrqbrr9, .table th.tinytable_css_5tvli6xsyv4whlrqbrr9 { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_lsicsdfkx5d3j2mqoqhj, .table th.tinytable_css_lsicsdfkx5d3j2mqoqhj { text-align: right; }
      .table td.tinytable_css_17zp1pn50jm9oxcbhtn9, .table th.tinytable_css_17zp1pn50jm9oxcbhtn9 { text-align: right; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_ycl1e1kbbuo9cjgh0xp3, .table th.tinytable_css_ycl1e1kbbuo9cjgh0xp3 { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_15ws2awlvk1mb2xvmf0c, .table th.tinytable_css_15ws2awlvk1mb2xvmf0c { text-align: left; }
      .table td.tinytable_css_y22e3n01ibs1ctf0wnkr, .table th.tinytable_css_y22e3n01ibs1ctf0wnkr { text-align: left; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_81ulk59x9efol9oeq5kj" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead><tr>
<th scope="col" data-row="0" data-col="0">z</th>
                <th scope="col" data-row="0" data-col="1">N</th>
                <th scope="col" data-row="0" data-col="2">Percent</th>
              </tr></thead>
<tbody>
<tr>
<td data-row="1" data-col="0">0</td>
                  <td data-row="1" data-col="1">4</td>
                  <td data-row="1" data-col="2">66.67</td>
                </tr>
<tr>
<td data-row="2" data-col="0">1</td>
                  <td data-row="2" data-col="1">2</td>
                  <td data-row="2" data-col="2">33.33</td>
                </tr>
</tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
</section></section><section id="add-rows" class="level2"><h2 class="anchored" data-anchor-id="add-rows">Add rows</h2>
<div class="cell">
<div class="sourceCode" id="cb54"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va">new_rows</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html">data.frame</a></span><span class="op">(</span><span class="st">'Does'</span>,</span>
<span>                       <span class="fl">2</span>,</span>
<span>                       <span class="st">'plus'</span>,</span>
<span>                       <span class="fl">2</span>,</span>
<span>                       <span class="st">'equals'</span>,</span>
<span>                       <span class="fl">5</span>,</span>
<span>                       <span class="st">'?'</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">flipper_length_mm</span> <span class="op">+</span> <span class="va">body_mass_g</span> <span class="op">~</span> <span class="va">species</span> <span class="op">*</span> <span class="op">(</span><span class="va">Mean</span> <span class="op">+</span> <span class="va">SD</span><span class="op">)</span>,</span>
<span>            data <span class="op">=</span> <span class="va">penguins</span>,</span>
<span>            add_rows <span class="op">=</span> <span class="va">new_rows</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_l9ly06wpltquvwb1qu57(i, j, css_id) {
          var table = document.getElementById("tinytable_l9ly06wpltquvwb1qu57");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_l9ly06wpltquvwb1qu57(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_l9ly06wpltquvwb1qu57");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '-1', j: 6 }, { i: '-1', j: 4 }, { i: '-1', j: 5 },  ], css_id: 'tinytable_css_cuc5yp6h3gwkuww3jnac',}, 
          { positions: [ { i: '3', j: 1 }, { i: '3', j: 2 }, { i: '3', j: 3 }, { i: '3', j: 5 }, { i: '3', j: 6 }, { i: '3', j: 4 },  ], css_id: 'tinytable_css_xkq1zmeku5ba4okiodt5',}, 
          { positions: [ { i: '2', j: 2 }, { i: '1', j: 1 }, { i: '2', j: 1 }, { i: '1', j: 3 }, { i: '2', j: 3 }, { i: '2', j: 4 }, { i: '1', j: 2 }, { i: '2', j: 6 }, { i: '1', j: 4 }, { i: '1', j: 5 }, { i: '2', j: 5 }, { i: '1', j: 6 },  ], css_id: 'tinytable_css_8g3sc15bb719xkenqtb1',}, 
          { positions: [ { i: '-1', j: 2 }, { i: '-1', j: 3 }, { i: '-1', j: 1 },  ], css_id: 'tinytable_css_t3250pvvbvbu1apxbiu1',}, 
          { positions: [ { i: '0', j: 1 }, { i: '0', j: 2 }, { i: '0', j: 3 }, { i: '0', j: 4 }, { i: '0', j: 5 }, { i: '0', j: 6 },  ], css_id: 'tinytable_css_7sd7drfc1vqv2jbmcp67',}, 
          { positions: [ { i: '3', j: 0 },  ], css_id: 'tinytable_css_ofy3x01wcof1m5rpve35',}, 
          { positions: [ { i: '1', j: 0 }, { i: '2', j: 0 },  ], css_id: 'tinytable_css_blpgr38fgu4hpyb4w145',}, 
          { positions: [ { i: '-1', j: 0 },  ], css_id: 'tinytable_css_0k3acjmmqjfhw9w4gsuu',}, 
          { positions: [ { i: '0', j: 0 },  ], css_id: 'tinytable_css_ol0sd4y5yg2ih5nukr7j',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_l9ly06wpltquvwb1qu57(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_cuc5yp6h3gwkuww3jnac, .table th.tinytable_css_cuc5yp6h3gwkuww3jnac { text-align: right; text-align: center; border-top: solid #d3d8dc 0.1em; text-align: center; }
      .table td.tinytable_css_xkq1zmeku5ba4okiodt5, .table th.tinytable_css_xkq1zmeku5ba4okiodt5 { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_8g3sc15bb719xkenqtb1, .table th.tinytable_css_8g3sc15bb719xkenqtb1 { text-align: right; }
      .table td.tinytable_css_t3250pvvbvbu1apxbiu1, .table th.tinytable_css_t3250pvvbvbu1apxbiu1 { text-align: right; text-align: center; border-top: solid #d3d8dc 0.1em; text-align: center; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_7sd7drfc1vqv2jbmcp67, .table th.tinytable_css_7sd7drfc1vqv2jbmcp67 { text-align: right; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_ofy3x01wcof1m5rpve35, .table th.tinytable_css_ofy3x01wcof1m5rpve35 { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_blpgr38fgu4hpyb4w145, .table th.tinytable_css_blpgr38fgu4hpyb4w145 { text-align: left; }
      .table td.tinytable_css_0k3acjmmqjfhw9w4gsuu, .table th.tinytable_css_0k3acjmmqjfhw9w4gsuu { text-align: left; text-align: center; border-top: solid #d3d8dc 0.1em; text-align: center; }
      .table td.tinytable_css_ol0sd4y5yg2ih5nukr7j, .table th.tinytable_css_ol0sd4y5yg2ih5nukr7j { text-align: left; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_l9ly06wpltquvwb1qu57" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead>
<tr>
<th scope="col" align="center" colspan="1" data-row="-1" data-col="0"> </th>
<th scope="col" align="center" colspan="2" data-row="-1" data-col="1">Adelie</th>
<th scope="col" align="center" colspan="2" data-row="-1" data-col="2">Chinstrap</th>
<th scope="col" align="center" colspan="2" data-row="-1" data-col="3">Gentoo</th>
</tr>
<tr>
<th scope="col" data-row="0" data-col="0"> </th>
                <th scope="col" data-row="0" data-col="1">Mean</th>
                <th scope="col" data-row="0" data-col="2">SD</th>
                <th scope="col" data-row="0" data-col="3">Mean</th>
                <th scope="col" data-row="0" data-col="4">SD</th>
                <th scope="col" data-row="0" data-col="5">Mean</th>
                <th scope="col" data-row="0" data-col="6">SD</th>
              </tr>
</thead>
<tbody>
<tr>
<td data-row="1" data-col="0">flipper_length_mm</td>
                  <td data-row="1" data-col="1">189.95</td>
                  <td data-row="1" data-col="2">6.54</td>
                  <td data-row="1" data-col="3">195.82</td>
                  <td data-row="1" data-col="4">7.13</td>
                  <td data-row="1" data-col="5">217.19</td>
                  <td data-row="1" data-col="6">6.48</td>
                </tr>
<tr>
<td data-row="2" data-col="0">body_mass_g</td>
                  <td data-row="2" data-col="1">3700.66</td>
                  <td data-row="2" data-col="2">458.57</td>
                  <td data-row="2" data-col="3">3733.09</td>
                  <td data-row="2" data-col="4">384.34</td>
                  <td data-row="2" data-col="5">5076.02</td>
                  <td data-row="2" data-col="6">504.12</td>
                </tr>
<tr>
<td data-row="3" data-col="0">Does</td>
                  <td data-row="3" data-col="1">2.00</td>
                  <td data-row="3" data-col="2">plus</td>
                  <td data-row="3" data-col="3">2.00</td>
                  <td data-row="3" data-col="4">equals</td>
                  <td data-row="3" data-col="5">5.00</td>
                  <td data-row="3" data-col="6">?</td>
                </tr>
</tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
</section><section id="add-columns" class="level2"><h2 class="anchored" data-anchor-id="add-columns">Add columns</h2>
<div class="cell">
<div class="sourceCode" id="cb55"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va">new_cols</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html">data.frame</a></span><span class="op">(</span><span class="st">'New Stat'</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/stats/Uniform.html">runif</a></span><span class="op">(</span><span class="fl">2</span><span class="op">)</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">flipper_length_mm</span> <span class="op">+</span> <span class="va">body_mass_g</span> <span class="op">~</span> <span class="va">species</span> <span class="op">*</span> <span class="op">(</span><span class="va">Mean</span> <span class="op">+</span> <span class="va">SD</span><span class="op">)</span>,</span>
<span>            data <span class="op">=</span> <span class="va">penguins</span>,</span>
<span>            add_columns <span class="op">=</span> <span class="va">new_cols</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_8hthveq5rzx4eld8dcvi(i, j, css_id) {
          var table = document.getElementById("tinytable_8hthveq5rzx4eld8dcvi");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_8hthveq5rzx4eld8dcvi(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_8hthveq5rzx4eld8dcvi");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '-1', j: 4 }, { i: '-1', j: 5 }, { i: '-1', j: 6 }, { i: '-1', j: 7 },  ], css_id: 'tinytable_css_yuy80fs1q6mz76wxwglz',}, 
          { positions: [ { i: '2', j: 1 }, { i: '2', j: 2 }, { i: '2', j: 3 }, { i: '2', j: 4 }, { i: '2', j: 5 }, { i: '2', j: 6 }, { i: '2', j: 7 },  ], css_id: 'tinytable_css_6y22xzjmqeal9qarutzw',}, 
          { positions: [ { i: '1', j: 1 }, { i: '1', j: 2 }, { i: '1', j: 3 }, { i: '1', j: 4 }, { i: '1', j: 5 }, { i: '1', j: 6 }, { i: '1', j: 7 },  ], css_id: 'tinytable_css_i7epbq4iajyqu5tkbxgw',}, 
          { positions: [ { i: '-1', j: 1 }, { i: '-1', j: 2 }, { i: '-1', j: 3 },  ], css_id: 'tinytable_css_qbazohbduczmh390o4d6',}, 
          { positions: [ { i: '0', j: 1 }, { i: '0', j: 2 }, { i: '0', j: 3 }, { i: '0', j: 4 }, { i: '0', j: 5 }, { i: '0', j: 6 }, { i: '0', j: 7 },  ], css_id: 'tinytable_css_3vxwozhzmlqd3kly91ws',}, 
          { positions: [ { i: '2', j: 0 },  ], css_id: 'tinytable_css_u4r223kffq8yspqgh9qb',}, 
          { positions: [ { i: '1', j: 0 },  ], css_id: 'tinytable_css_zchwztgg9hqrnufwga6c',}, 
          { positions: [ { i: '-1', j: 0 },  ], css_id: 'tinytable_css_he3uhvw6k4w5cae46jwc',}, 
          { positions: [ { i: '0', j: 0 },  ], css_id: 'tinytable_css_hw13i7bpcrff1rh7c64q',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_8hthveq5rzx4eld8dcvi(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_yuy80fs1q6mz76wxwglz, .table th.tinytable_css_yuy80fs1q6mz76wxwglz { text-align: right; text-align: center; border-top: solid #d3d8dc 0.1em; text-align: center; }
      .table td.tinytable_css_6y22xzjmqeal9qarutzw, .table th.tinytable_css_6y22xzjmqeal9qarutzw { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_i7epbq4iajyqu5tkbxgw, .table th.tinytable_css_i7epbq4iajyqu5tkbxgw { text-align: right; }
      .table td.tinytable_css_qbazohbduczmh390o4d6, .table th.tinytable_css_qbazohbduczmh390o4d6 { text-align: right; text-align: center; border-top: solid #d3d8dc 0.1em; text-align: center; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_3vxwozhzmlqd3kly91ws, .table th.tinytable_css_3vxwozhzmlqd3kly91ws { text-align: right; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_u4r223kffq8yspqgh9qb, .table th.tinytable_css_u4r223kffq8yspqgh9qb { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_zchwztgg9hqrnufwga6c, .table th.tinytable_css_zchwztgg9hqrnufwga6c { text-align: left; }
      .table td.tinytable_css_he3uhvw6k4w5cae46jwc, .table th.tinytable_css_he3uhvw6k4w5cae46jwc { text-align: left; text-align: center; border-top: solid #d3d8dc 0.1em; text-align: center; }
      .table td.tinytable_css_hw13i7bpcrff1rh7c64q, .table th.tinytable_css_hw13i7bpcrff1rh7c64q { text-align: left; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_8hthveq5rzx4eld8dcvi" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead>
<tr>
<th scope="col" align="center" colspan="1" data-row="-1" data-col="0"> </th>
<th scope="col" align="center" colspan="2" data-row="-1" data-col="1">Adelie</th>
<th scope="col" align="center" colspan="2" data-row="-1" data-col="2">Chinstrap</th>
<th scope="col" align="center" colspan="2" data-row="-1" data-col="3">Gentoo</th>
<th scope="col" align="center" colspan="1" data-row="-1" data-col="4"> </th>
</tr>
<tr>
<th scope="col" data-row="0" data-col="0"> </th>
                <th scope="col" data-row="0" data-col="1">Mean</th>
                <th scope="col" data-row="0" data-col="2">SD</th>
                <th scope="col" data-row="0" data-col="3">Mean</th>
                <th scope="col" data-row="0" data-col="4">SD</th>
                <th scope="col" data-row="0" data-col="5">Mean</th>
                <th scope="col" data-row="0" data-col="6">SD</th>
                <th scope="col" data-row="0" data-col="7">New.Stat</th>
              </tr>
</thead>
<tbody>
<tr>
<td data-row="1" data-col="0">flipper_length_mm</td>
                  <td data-row="1" data-col="1">189.95</td>
                  <td data-row="1" data-col="2">6.54</td>
                  <td data-row="1" data-col="3">195.82</td>
                  <td data-row="1" data-col="4">7.13</td>
                  <td data-row="1" data-col="5">217.19</td>
                  <td data-row="1" data-col="6">6.48</td>
                  <td data-row="1" data-col="7">0.20</td>
                </tr>
<tr>
<td data-row="2" data-col="0">body_mass_g</td>
                  <td data-row="2" data-col="1">3700.66</td>
                  <td data-row="2" data-col="2">458.57</td>
                  <td data-row="2" data-col="3">3733.09</td>
                  <td data-row="2" data-col="4">384.34</td>
                  <td data-row="2" data-col="5">5076.02</td>
                  <td data-row="2" data-col="6">504.12</td>
                  <td data-row="2" data-col="7">0.80</td>
                </tr>
</tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
</section><section id="rounding" class="level2"><h2 class="anchored" data-anchor-id="rounding">Rounding</h2>
<p>The <code>fmt</code> argument allows us to set the printing format of numeric values. It accepts a single number representing the number of digits after the period, or a string to be passed to the <code>sprintf</code> function. For instance, setting <code>fmt="%.4f"</code> will keep 4 digits after the dot (see <code><a href="https://rdrr.io/r/base/sprintf.html">?sprintf</a></code> for more options):</p>
<div class="cell">
<div class="sourceCode" id="cb56"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">flipper_length_mm</span> <span class="op">~</span> <span class="va">Mean</span> <span class="op">+</span> <span class="va">SD</span>,</span>
<span>            fmt <span class="op">=</span> <span class="fl">4</span>,</span>
<span>            data <span class="op">=</span> <span class="va">penguins</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_ubx5177erd7u2wgmtkjs(i, j, css_id) {
          var table = document.getElementById("tinytable_ubx5177erd7u2wgmtkjs");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_ubx5177erd7u2wgmtkjs(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_ubx5177erd7u2wgmtkjs");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '1', j: 1 }, { i: '1', j: 2 },  ], css_id: 'tinytable_css_0izowebe19kfuepqydsz',}, 
          { positions: [ { i: '0', j: 1 }, { i: '0', j: 2 },  ], css_id: 'tinytable_css_0gww9hxnlyqbizunc6ed',}, 
          { positions: [ { i: '1', j: 0 },  ], css_id: 'tinytable_css_i4mk4gs39mf304ifuog0',}, 
          { positions: [ { i: '0', j: 0 },  ], css_id: 'tinytable_css_k3nasdpurnrbkhlebsxd',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_ubx5177erd7u2wgmtkjs(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_0izowebe19kfuepqydsz, .table th.tinytable_css_0izowebe19kfuepqydsz { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_0gww9hxnlyqbizunc6ed, .table th.tinytable_css_0gww9hxnlyqbizunc6ed { text-align: right; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_i4mk4gs39mf304ifuog0, .table th.tinytable_css_i4mk4gs39mf304ifuog0 { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_k3nasdpurnrbkhlebsxd, .table th.tinytable_css_k3nasdpurnrbkhlebsxd { text-align: left; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_ubx5177erd7u2wgmtkjs" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead><tr>
<th scope="col" data-row="0" data-col="0"> </th>
                <th scope="col" data-row="0" data-col="1">Mean</th>
                <th scope="col" data-row="0" data-col="2">SD</th>
              </tr></thead>
<tbody><tr>
<td data-row="1" data-col="0">flipper_length_mm</td>
                  <td data-row="1" data-col="1">200.9152</td>
                  <td data-row="1" data-col="2">14.0617</td>
                </tr></tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
<p>We can set the formatting on a term-by-term basis by using the same <code>Arguments</code> function that we used to handle missing values in the previous section. The shortcut functions that ship with <code>modelsummary</code> (e.g., <code>Mean</code>, <code>SD</code>, <code>Median</code>, <code>P25</code>) all include a <code>fmt</code> argument:</p>
<div class="cell">
<div class="sourceCode" id="cb57"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">flipper_length_mm</span> <span class="op">~</span> <span class="va">Mean</span> <span class="op">*</span> <span class="fu"><a href="https://dmurdoch.github.io/tables/reference/Arguments.html">Arguments</a></span><span class="op">(</span>fmt <span class="op">=</span> <span class="st">"%.4f"</span><span class="op">)</span> <span class="op">+</span> <span class="va">SD</span> <span class="op">*</span> <span class="fu"><a href="https://dmurdoch.github.io/tables/reference/Arguments.html">Arguments</a></span><span class="op">(</span>fmt <span class="op">=</span> <span class="st">"%.1f"</span><span class="op">)</span>,</span>
<span>            data <span class="op">=</span> <span class="va">penguins</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_plonie5netyvcwfucxag(i, j, css_id) {
          var table = document.getElementById("tinytable_plonie5netyvcwfucxag");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_plonie5netyvcwfucxag(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_plonie5netyvcwfucxag");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '1', j: 1 }, { i: '1', j: 2 },  ], css_id: 'tinytable_css_778nexqb90eeai9lyxnt',}, 
          { positions: [ { i: '0', j: 1 }, { i: '0', j: 2 },  ], css_id: 'tinytable_css_en2xiijfj8btug9zw8uo',}, 
          { positions: [ { i: '1', j: 0 },  ], css_id: 'tinytable_css_k20ohf9uhpu5el66t0rc',}, 
          { positions: [ { i: '0', j: 0 },  ], css_id: 'tinytable_css_ciryj88487ankl9cre9b',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_plonie5netyvcwfucxag(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_778nexqb90eeai9lyxnt, .table th.tinytable_css_778nexqb90eeai9lyxnt { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_en2xiijfj8btug9zw8uo, .table th.tinytable_css_en2xiijfj8btug9zw8uo { text-align: right; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_k20ohf9uhpu5el66t0rc, .table th.tinytable_css_k20ohf9uhpu5el66t0rc { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_ciryj88487ankl9cre9b, .table th.tinytable_css_ciryj88487ankl9cre9b { text-align: left; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_plonie5netyvcwfucxag" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead><tr>
<th scope="col" data-row="0" data-col="0"> </th>
                <th scope="col" data-row="0" data-col="1">Mean</th>
                <th scope="col" data-row="0" data-col="2">SD</th>
              </tr></thead>
<tbody><tr>
<td data-row="1" data-col="0">flipper_length_mm</td>
                  <td data-row="1" data-col="1">200.9152</td>
                  <td data-row="1" data-col="2">14.1</td>
                </tr></tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
<p>Note that the <code><a href="https://modelsummary.com/man/N.html">N()</a></code> function is supplied by the upstream <code>tables</code> package, and does not have a <code>fmt</code> argument. Fortunately, it is easy to override the built-in function to use custom formatting:</p>
<div class="cell">
<div class="sourceCode" id="cb58"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va">tmp</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html">data.frame</a></span><span class="op">(</span>X <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/sample.html">sample</a></span><span class="op">(</span><span class="va">letters</span><span class="op">[</span><span class="fl">1</span><span class="op">:</span><span class="fl">3</span><span class="op">]</span>, <span class="fl">1e6</span>, replace <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span><span class="op">)</span></span>
<span><span class="va">N</span> <span class="op">&lt;-</span> \<span class="op">(</span><span class="va">x</span><span class="op">)</span> <span class="fu"><a href="https://rdrr.io/r/base/format.html">format</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/length.html">length</a></span><span class="op">(</span><span class="va">x</span><span class="op">)</span>, big.mark <span class="op">=</span> <span class="st">","</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">X</span> <span class="op">~</span> <span class="va">N</span>, data <span class="op">=</span> <span class="va">tmp</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_o2o2sdbndbib2ptlc1m6(i, j, css_id) {
          var table = document.getElementById("tinytable_o2o2sdbndbib2ptlc1m6");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_o2o2sdbndbib2ptlc1m6(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_o2o2sdbndbib2ptlc1m6");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '3', j: 1 },  ], css_id: 'tinytable_css_ld3f0wfv0zt8gggof1u1',}, 
          { positions: [ { i: '1', j: 1 }, { i: '2', j: 1 },  ], css_id: 'tinytable_css_jfhmqr2e6qkm7h1fw8bz',}, 
          { positions: [ { i: '0', j: 1 },  ], css_id: 'tinytable_css_3ublaw464ep05jyl35sq',}, 
          { positions: [ { i: '3', j: 0 },  ], css_id: 'tinytable_css_axorzxhwjgiskn1r5p1l',}, 
          { positions: [ { i: '1', j: 0 }, { i: '2', j: 0 },  ], css_id: 'tinytable_css_damve5jdxy965qg1e1y6',}, 
          { positions: [ { i: '0', j: 0 },  ], css_id: 'tinytable_css_zthsxxbo67cr4ibrtdnv',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_o2o2sdbndbib2ptlc1m6(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_ld3f0wfv0zt8gggof1u1, .table th.tinytable_css_ld3f0wfv0zt8gggof1u1 { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_jfhmqr2e6qkm7h1fw8bz, .table th.tinytable_css_jfhmqr2e6qkm7h1fw8bz { text-align: right; }
      .table td.tinytable_css_3ublaw464ep05jyl35sq, .table th.tinytable_css_3ublaw464ep05jyl35sq { text-align: right; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_axorzxhwjgiskn1r5p1l, .table th.tinytable_css_axorzxhwjgiskn1r5p1l { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_damve5jdxy965qg1e1y6, .table th.tinytable_css_damve5jdxy965qg1e1y6 { text-align: left; }
      .table td.tinytable_css_zthsxxbo67cr4ibrtdnv, .table th.tinytable_css_zthsxxbo67cr4ibrtdnv { text-align: left; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_o2o2sdbndbib2ptlc1m6" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead><tr>
<th scope="col" data-row="0" data-col="0">X</th>
                <th scope="col" data-row="0" data-col="1">N</th>
              </tr></thead>
<tbody>
<tr>
<td data-row="1" data-col="0">a</td>
                  <td data-row="1" data-col="1">332,520</td>
                </tr>
<tr>
<td data-row="2" data-col="0">b</td>
                  <td data-row="2" data-col="1">333,560</td>
                </tr>
<tr>
<td data-row="3" data-col="0">c</td>
                  <td data-row="3" data-col="1">333,920</td>
                </tr>
</tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
</section></section><section id="titles-and-notes" class="level1"><h1>Titles and notes</h1>
<p><code>datasummary</code> includes the same <code>title</code> and <code>notes</code> arguments as in <code>modelsummary</code>:</p>
<div class="cell">
<div class="sourceCode" id="cb59"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="fu"><a href="https://dmurdoch.github.io/tables/reference/All.html">All</a></span><span class="op">(</span><span class="va">penguins</span><span class="op">)</span> <span class="op">~</span> <span class="va">Mean</span> <span class="op">+</span> <span class="va">SD</span>,</span>
<span>            data <span class="op">=</span> <span class="va">penguins</span>,</span>
<span>            title <span class="op">=</span> <span class="st">'Statistics about the famous Palmer Penguins.'</span>,</span>
<span>            notes <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">'A note at the bottom of the table.'</span><span class="op">)</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_duwo7xnkt72ts31thu2l(i, j, css_id) {
          var table = document.getElementById("tinytable_duwo7xnkt72ts31thu2l");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_duwo7xnkt72ts31thu2l(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_duwo7xnkt72ts31thu2l");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '6', j: 1 }, { i: '6', j: 2 },  ], css_id: 'tinytable_css_81qf5zseqvixylhqfix5',}, 
          { positions: [ { i: '1', j: 1 }, { i: '2', j: 1 }, { i: '4', j: 1 }, { i: '5', j: 1 }, { i: '3', j: 2 }, { i: '3', j: 1 }, { i: '1', j: 2 }, { i: '2', j: 2 }, { i: '4', j: 2 }, { i: '5', j: 2 },  ], css_id: 'tinytable_css_d9chj4w8vxbf90dl9or0',}, 
          { positions: [ { i: '0', j: 1 }, { i: '0', j: 2 },  ], css_id: 'tinytable_css_nlcyuilm99u7vt03cb1s',}, 
          { positions: [ { i: '6', j: 0 },  ], css_id: 'tinytable_css_60w80bykbqzp4cn5c4jc',}, 
          { positions: [ { i: '1', j: 0 }, { i: '2', j: 0 }, { i: '3', j: 0 }, { i: '4', j: 0 }, { i: '5', j: 0 },  ], css_id: 'tinytable_css_u7erkipf7f45jetkbwu7',}, 
          { positions: [ { i: '0', j: 0 },  ], css_id: 'tinytable_css_ypqn6ym33fho5c3oi1r3',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_duwo7xnkt72ts31thu2l(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_81qf5zseqvixylhqfix5, .table th.tinytable_css_81qf5zseqvixylhqfix5 { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_d9chj4w8vxbf90dl9or0, .table th.tinytable_css_d9chj4w8vxbf90dl9or0 { text-align: right; }
      .table td.tinytable_css_nlcyuilm99u7vt03cb1s, .table th.tinytable_css_nlcyuilm99u7vt03cb1s { text-align: right; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_60w80bykbqzp4cn5c4jc, .table th.tinytable_css_60w80bykbqzp4cn5c4jc { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_u7erkipf7f45jetkbwu7, .table th.tinytable_css_u7erkipf7f45jetkbwu7 { text-align: left; }
      .table td.tinytable_css_ypqn6ym33fho5c3oi1r3, .table th.tinytable_css_ypqn6ym33fho5c3oi1r3 { text-align: left; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_duwo7xnkt72ts31thu2l" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead>
</thead><caption>Statistics about the famous Palmer Penguins.</caption>
              <tbody><tr>
<th scope="col" data-row="0" data-col="0"> </th>
                <th scope="col" data-row="0" data-col="1">Mean</th>
                <th scope="col" data-row="0" data-col="2">SD</th>
              </tr>

</tbody><tfoot><tr><td colspan="3">A note at the bottom of the table.</td></tr></tfoot>
<tbody>
<tr>
<td data-row="1" data-col="0">rownames</td>
                  <td data-row="1" data-col="1">172.50</td>
                  <td data-row="1" data-col="2">99.45</td>
                </tr>
<tr>
<td data-row="2" data-col="0">bill_length_mm</td>
                  <td data-row="2" data-col="1">43.92</td>
                  <td data-row="2" data-col="2">5.46</td>
                </tr>
<tr>
<td data-row="3" data-col="0">bill_depth_mm</td>
                  <td data-row="3" data-col="1">17.15</td>
                  <td data-row="3" data-col="2">1.97</td>
                </tr>
<tr>
<td data-row="4" data-col="0">flipper_length_mm</td>
                  <td data-row="4" data-col="1">200.92</td>
                  <td data-row="4" data-col="2">14.06</td>
                </tr>
<tr>
<td data-row="5" data-col="0">body_mass_g</td>
                  <td data-row="5" data-col="1">4201.75</td>
                  <td data-row="5" data-col="2">801.95</td>
                </tr>
<tr>
<td data-row="6" data-col="0">year</td>
                  <td data-row="6" data-col="1">2008.03</td>
                  <td data-row="6" data-col="2">0.82</td>
                </tr>
</tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
<section id="alignment" class="level2"><h2 class="anchored" data-anchor-id="alignment">Alignment</h2>
<p>We can align columns using the <code>align</code> argument. <code>align</code> should be a string of length equal to the number of columns, and which includes only the letters “l”, “c”, or “r”:</p>
<div class="cell">
<div class="sourceCode" id="cb60"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">flipper_length_mm</span> <span class="op">+</span> <span class="va">bill_length_mm</span> <span class="op">~</span> <span class="va">Mean</span> <span class="op">+</span> <span class="va">SD</span> <span class="op">+</span> <span class="va">Range</span>,</span>
<span>            data <span class="op">=</span> <span class="va">penguins</span>,</span>
<span>            align <span class="op">=</span> <span class="st">'lrcl'</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_spam0v8n50opw06xh6uk(i, j, css_id) {
          var table = document.getElementById("tinytable_spam0v8n50opw06xh6uk");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_spam0v8n50opw06xh6uk(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_spam0v8n50opw06xh6uk");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '2', j: 2 },  ], css_id: 'tinytable_css_wpv8snoyrad519l61w79',}, 
          { positions: [ { i: '1', j: 2 },  ], css_id: 'tinytable_css_mq84rn8hiv98kjlfbwzm',}, 
          { positions: [ { i: '0', j: 2 },  ], css_id: 'tinytable_css_rgcwtabuqxeywlroakxp',}, 
          { positions: [ { i: '2', j: 1 },  ], css_id: 'tinytable_css_0va0tagvrwxqdwrhrl38',}, 
          { positions: [ { i: '1', j: 1 },  ], css_id: 'tinytable_css_ddihxbgbg0uavo0i8eei',}, 
          { positions: [ { i: '0', j: 1 },  ], css_id: 'tinytable_css_yc7njoiv0k3p4cr4jnpf',}, 
          { positions: [ { i: '2', j: 3 }, { i: '2', j: 0 },  ], css_id: 'tinytable_css_a150l5phvk4d298p51b6',}, 
          { positions: [ { i: '1', j: 3 }, { i: '1', j: 0 },  ], css_id: 'tinytable_css_lzorn4qnet9bulc6xm10',}, 
          { positions: [ { i: '0', j: 0 }, { i: '0', j: 3 },  ], css_id: 'tinytable_css_ejxuoh498hbdszv0gk8e',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_spam0v8n50opw06xh6uk(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_wpv8snoyrad519l61w79, .table th.tinytable_css_wpv8snoyrad519l61w79 { text-align: center; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_mq84rn8hiv98kjlfbwzm, .table th.tinytable_css_mq84rn8hiv98kjlfbwzm { text-align: center; }
      .table td.tinytable_css_rgcwtabuqxeywlroakxp, .table th.tinytable_css_rgcwtabuqxeywlroakxp { text-align: center; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_0va0tagvrwxqdwrhrl38, .table th.tinytable_css_0va0tagvrwxqdwrhrl38 { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_ddihxbgbg0uavo0i8eei, .table th.tinytable_css_ddihxbgbg0uavo0i8eei { text-align: right; }
      .table td.tinytable_css_yc7njoiv0k3p4cr4jnpf, .table th.tinytable_css_yc7njoiv0k3p4cr4jnpf { text-align: right; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_a150l5phvk4d298p51b6, .table th.tinytable_css_a150l5phvk4d298p51b6 { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_lzorn4qnet9bulc6xm10, .table th.tinytable_css_lzorn4qnet9bulc6xm10 { text-align: left; }
      .table td.tinytable_css_ejxuoh498hbdszv0gk8e, .table th.tinytable_css_ejxuoh498hbdszv0gk8e { text-align: left; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_spam0v8n50opw06xh6uk" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead><tr>
<th scope="col" data-row="0" data-col="0"> </th>
                <th scope="col" data-row="0" data-col="1">Mean</th>
                <th scope="col" data-row="0" data-col="2">SD</th>
                <th scope="col" data-row="0" data-col="3">Range</th>
              </tr></thead>
<tbody>
<tr>
<td data-row="1" data-col="0">flipper_length_mm</td>
                  <td data-row="1" data-col="1">200.92</td>
                  <td data-row="1" data-col="2">14.06</td>
                  <td data-row="1" data-col="3">59.00</td>
                </tr>
<tr>
<td data-row="2" data-col="0">bill_length_mm</td>
                  <td data-row="2" data-col="1">43.92</td>
                  <td data-row="2" data-col="2">5.46</td>
                  <td data-row="2" data-col="3">27.50</td>
                </tr>
</tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
</section><section id="factors" class="level2"><h2 class="anchored" data-anchor-id="factors">Factors</h2>
<p>By default, the <code>factor</code> function in <code>R</code> does <em>not</em> assign a distinct factor level to missing values: the <code>factor</code> function’s <code>exclude</code> argument is set to <code>NA</code> by default. To ensure that <code>NA</code>s appear in your table, make sure you set <code>exclude=NULL</code> when you create the factor.</p>
<p>Internally, the <code>datasummary_balance</code> and <code>datasummary_crosstab</code> functions convert logical and character variables to factor with the <code>exclude=NULL</code> argument. This means that <code>NA</code>s will appear in the table as distinct rows/columns. If you do <em>not</em> want <code>NA</code>s to appear in your table, convert them to factors yourself ahead of time. For example:</p>
<div class="cell">
<div class="sourceCode" id="cb61"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va">mycars</span> <span class="op">&lt;-</span> <span class="va">mtcars</span><span class="op">[</span>, <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"cyl"</span>, <span class="st">"mpg"</span>, <span class="st">"hp"</span>, <span class="st">"vs"</span><span class="op">)</span><span class="op">]</span></span>
<span><span class="va">mycars</span><span class="op">$</span><span class="va">cyl</span><span class="op">[</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">1</span>, <span class="fl">3</span>, <span class="fl">6</span>, <span class="fl">8</span><span class="op">)</span><span class="op">]</span> <span class="op">&lt;-</span> <span class="cn">NA</span></span>
<span><span class="va">mycars</span><span class="op">$</span><span class="va">cyl_nona</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/factor.html">factor</a></span><span class="op">(</span><span class="va">mycars</span><span class="op">$</span><span class="va">cyl</span><span class="op">)</span></span>
<span><span class="va">mycars</span><span class="op">$</span><span class="va">cyl_na</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/factor.html">factor</a></span><span class="op">(</span><span class="va">mycars</span><span class="op">$</span><span class="va">cyl</span>, exclude <span class="op">=</span> <span class="cn">NULL</span><span class="op">)</span></span>
<span></span>
<span><span class="fu"><a href="https://modelsummary.com/man/datasummary_crosstab.html">datasummary_crosstab</a></span><span class="op">(</span><span class="va">cyl_nona</span> <span class="op">~</span> <span class="va">vs</span>, data <span class="op">=</span> <span class="va">mycars</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_cf0w65jrquboevtlp283(i, j, css_id) {
          var table = document.getElementById("tinytable_cf0w65jrquboevtlp283");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_cf0w65jrquboevtlp283(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_cf0w65jrquboevtlp283");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '8', j: 3 }, { i: '8', j: 2 }, { i: '8', j: 4 },  ], css_id: 'tinytable_css_u9mtgs4zn13visnluj0s',}, 
          { positions: [ { i: '2', j: 2 }, { i: '3', j: 2 }, { i: '4', j: 2 }, { i: '6', j: 2 }, { i: '7', j: 2 }, { i: '3', j: 4 }, { i: '1', j: 2 }, { i: '1', j: 3 }, { i: '2', j: 3 }, { i: '3', j: 3 }, { i: '4', j: 3 }, { i: '5', j: 3 }, { i: '6', j: 3 }, { i: '7', j: 3 }, { i: '7', j: 4 }, { i: '5', j: 2 }, { i: '1', j: 4 }, { i: '2', j: 4 }, { i: '4', j: 4 }, { i: '5', j: 4 }, { i: '6', j: 4 },  ], css_id: 'tinytable_css_fywonkqrfq337c680cxo',}, 
          { positions: [ { i: '0', j: 2 }, { i: '0', j: 4 }, { i: '0', j: 3 },  ], css_id: 'tinytable_css_w078y062eyy2dwmy0jud',}, 
          { positions: [ { i: '8', j: 0 }, { i: '8', j: 1 },  ], css_id: 'tinytable_css_zl23ku285133ignfbqe8',}, 
          { positions: [ { i: '2', j: 0 }, { i: '3', j: 0 }, { i: '4', j: 0 }, { i: '1', j: 0 }, { i: '6', j: 0 }, { i: '7', j: 0 }, { i: '3', j: 1 }, { i: '5', j: 0 }, { i: '1', j: 1 }, { i: '2', j: 1 }, { i: '7', j: 1 }, { i: '4', j: 1 }, { i: '5', j: 1 }, { i: '6', j: 1 },  ], css_id: 'tinytable_css_6fafbnrwld36e4f6hxv8',}, 
          { positions: [ { i: '0', j: 0 }, { i: '0', j: 1 },  ], css_id: 'tinytable_css_nvc3aoftx1bkgdewq9lz',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_cf0w65jrquboevtlp283(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_u9mtgs4zn13visnluj0s, .table th.tinytable_css_u9mtgs4zn13visnluj0s { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_fywonkqrfq337c680cxo, .table th.tinytable_css_fywonkqrfq337c680cxo { text-align: right; }
      .table td.tinytable_css_w078y062eyy2dwmy0jud, .table th.tinytable_css_w078y062eyy2dwmy0jud { text-align: right; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_zl23ku285133ignfbqe8, .table th.tinytable_css_zl23ku285133ignfbqe8 { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_6fafbnrwld36e4f6hxv8, .table th.tinytable_css_6fafbnrwld36e4f6hxv8 { text-align: left; }
      .table td.tinytable_css_nvc3aoftx1bkgdewq9lz, .table th.tinytable_css_nvc3aoftx1bkgdewq9lz { text-align: left; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_cf0w65jrquboevtlp283" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead><tr>
<th scope="col" data-row="0" data-col="0">cyl_nona</th>
                <th scope="col" data-row="0" data-col="1"> </th>
                <th scope="col" data-row="0" data-col="2">0</th>
                <th scope="col" data-row="0" data-col="3">1</th>
                <th scope="col" data-row="0" data-col="4">All</th>
              </tr></thead>
<tbody>
<tr>
<td data-row="1" data-col="0">4</td>
                  <td data-row="1" data-col="1">N</td>
                  <td data-row="1" data-col="2">1</td>
                  <td data-row="1" data-col="3">8</td>
                  <td data-row="1" data-col="4">9</td>
                </tr>
<tr>
<td data-row="2" data-col="0"></td>
                  <td data-row="2" data-col="1">% row</td>
                  <td data-row="2" data-col="2">11.1</td>
                  <td data-row="2" data-col="3">88.9</td>
                  <td data-row="2" data-col="4">100.0</td>
                </tr>
<tr>
<td data-row="3" data-col="0">6</td>
                  <td data-row="3" data-col="1">N</td>
                  <td data-row="3" data-col="2">2</td>
                  <td data-row="3" data-col="3">3</td>
                  <td data-row="3" data-col="4">5</td>
                </tr>
<tr>
<td data-row="4" data-col="0"></td>
                  <td data-row="4" data-col="1">% row</td>
                  <td data-row="4" data-col="2">40.0</td>
                  <td data-row="4" data-col="3">60.0</td>
                  <td data-row="4" data-col="4">100.0</td>
                </tr>
<tr>
<td data-row="5" data-col="0">8</td>
                  <td data-row="5" data-col="1">N</td>
                  <td data-row="5" data-col="2">14</td>
                  <td data-row="5" data-col="3">0</td>
                  <td data-row="5" data-col="4">14</td>
                </tr>
<tr>
<td data-row="6" data-col="0"></td>
                  <td data-row="6" data-col="1">% row</td>
                  <td data-row="6" data-col="2">100.0</td>
                  <td data-row="6" data-col="3">0.0</td>
                  <td data-row="6" data-col="4">100.0</td>
                </tr>
<tr>
<td data-row="7" data-col="0">All</td>
                  <td data-row="7" data-col="1">N</td>
                  <td data-row="7" data-col="2">18</td>
                  <td data-row="7" data-col="3">14</td>
                  <td data-row="7" data-col="4">32</td>
                </tr>
<tr>
<td data-row="8" data-col="0"></td>
                  <td data-row="8" data-col="1">% row</td>
                  <td data-row="8" data-col="2">56.2</td>
                  <td data-row="8" data-col="3">43.8</td>
                  <td data-row="8" data-col="4">100.0</td>
                </tr>
</tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
<div class="sourceCode" id="cb62"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://modelsummary.com/man/datasummary_crosstab.html">datasummary_crosstab</a></span><span class="op">(</span><span class="va">cyl_na</span> <span class="op">~</span> <span class="va">vs</span>, data <span class="op">=</span> <span class="va">mycars</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<!-- preamble start -->

    <script>

      function styleCell_pixn880kxwxd426izb96(i, j, css_id) {
          var table = document.getElementById("tinytable_pixn880kxwxd426izb96");
          var cell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
          if (cell) {
              console.log(`Styling cell at (${i}, ${j}) with class ${css_id}`);
              cell.classList.add(css_id);
          } else {
              console.warn(`Cell at (${i}, ${j}) not found.`);
          }
      }
      function spanCell_pixn880kxwxd426izb96(i, j, rowspan, colspan) {
        var table = document.getElementById("tinytable_pixn880kxwxd426izb96");
        const targetCell = table.querySelector(`[data-row="${i}"][data-col="${j}"]`);
        if (!targetCell) {
          console.warn(`Cell at (${i}, ${j}) not found.`);
        }

        // Get all cells that need to be removed
        const cellsToRemove = [];
        for (let r = 0; r < rowspan; r++) {
          for (let c = 0; c < colspan; c++) {
            if (r === 0 && c === 0) continue; // Skip the target cell
            const cell = table.querySelector(`[data-row="${i + r}"][data-col="${j + c}"]`);
            if (cell) {
              cellsToRemove.push(cell);
            }
          }
        }

        // Remove all cells
        cellsToRemove.forEach(cell => cell.remove());

        // Set rowspan and colspan of the target cell if it exists
        if (targetCell) {
          targetCell.rowSpan = rowspan;
          targetCell.colSpan = colspan;
        }
      }
      // tinytable span after
      window.addEventListener('load', function () {
          var cellsToStyle = [
            // tinytable style arrays after
          { positions: [ { i: '10', j: 3 }, { i: '10', j: 2 }, { i: '10', j: 4 },  ], css_id: 'tinytable_css_gy2qkterj1ipw07zakcg',}, 
          { positions: [ { i: '2', j: 2 }, { i: '4', j: 2 }, { i: '1', j: 2 }, { i: '6', j: 2 }, { i: '3', j: 2 }, { i: '8', j: 2 }, { i: '5', j: 2 }, { i: '1', j: 4 }, { i: '7', j: 2 }, { i: '1', j: 3 }, { i: '2', j: 3 }, { i: '3', j: 3 }, { i: '4', j: 3 }, { i: '5', j: 3 }, { i: '6', j: 3 }, { i: '7', j: 3 }, { i: '8', j: 3 }, { i: '9', j: 3 }, { i: '3', j: 4 }, { i: '9', j: 2 }, { i: '5', j: 4 }, { i: '2', j: 4 }, { i: '7', j: 4 }, { i: '4', j: 4 }, { i: '9', j: 4 }, { i: '6', j: 4 }, { i: '8', j: 4 },  ], css_id: 'tinytable_css_yl7hyfl0wr2jmd4bb2xa',}, 
          { positions: [ { i: '0', j: 2 }, { i: '0', j: 4 }, { i: '0', j: 3 },  ], css_id: 'tinytable_css_c8g636o7fvvgnqwv50rq',}, 
          { positions: [ { i: '10', j: 0 }, { i: '10', j: 1 },  ], css_id: 'tinytable_css_w1a6omk9e03ormxszsbz',}, 
          { positions: [ { i: '1', j: 0 }, { i: '2', j: 0 }, { i: '4', j: 0 }, { i: '5', j: 0 }, { i: '6', j: 0 }, { i: '3', j: 0 }, { i: '8', j: 0 }, { i: '9', j: 0 }, { i: '3', j: 1 }, { i: '7', j: 0 }, { i: '1', j: 1 }, { i: '2', j: 1 }, { i: '7', j: 1 }, { i: '4', j: 1 }, { i: '5', j: 1 }, { i: '6', j: 1 }, { i: '8', j: 1 }, { i: '9', j: 1 },  ], css_id: 'tinytable_css_afa8s8kj67lnau9dy9rm',}, 
          { positions: [ { i: '0', j: 0 }, { i: '0', j: 1 },  ], css_id: 'tinytable_css_7sczbuydk097nyoojajn',}, 
          ];

          // Loop over the arrays to style the cells
          cellsToStyle.forEach(function (group) {
              group.positions.forEach(function (cell) {
                  styleCell_pixn880kxwxd426izb96(cell.i, cell.j, group.css_id);
              });
          });
      });
    </script><style>
      /* tinytable css entries after */
      .table td.tinytable_css_gy2qkterj1ipw07zakcg, .table th.tinytable_css_gy2qkterj1ipw07zakcg { text-align: right; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_yl7hyfl0wr2jmd4bb2xa, .table th.tinytable_css_yl7hyfl0wr2jmd4bb2xa { text-align: right; }
      .table td.tinytable_css_c8g636o7fvvgnqwv50rq, .table th.tinytable_css_c8g636o7fvvgnqwv50rq { text-align: right; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
      .table td.tinytable_css_w1a6omk9e03ormxszsbz, .table th.tinytable_css_w1a6omk9e03ormxszsbz { text-align: left; border-bottom: solid #d3d8dc 0.1em; }
      .table td.tinytable_css_afa8s8kj67lnau9dy9rm, .table th.tinytable_css_afa8s8kj67lnau9dy9rm { text-align: left; }
      .table td.tinytable_css_7sczbuydk097nyoojajn, .table th.tinytable_css_7sczbuydk097nyoojajn { text-align: left; border-top: solid #d3d8dc 0.1em; border-bottom: solid #d3d8dc 0.05em; }
    </style>
<div class="container">
      <table class="table table-borderless" id="tinytable_pixn880kxwxd426izb96" style="width: auto; margin-left: auto; margin-right: auto;" data-quarto-disable-processing="true">
<thead><tr>
<th scope="col" data-row="0" data-col="0">cyl_na</th>
                <th scope="col" data-row="0" data-col="1"> </th>
                <th scope="col" data-row="0" data-col="2">0</th>
                <th scope="col" data-row="0" data-col="3">1</th>
                <th scope="col" data-row="0" data-col="4">All</th>
              </tr></thead>
<tbody>
<tr>
<td data-row="1" data-col="0">4</td>
                  <td data-row="1" data-col="1">N</td>
                  <td data-row="1" data-col="2">1</td>
                  <td data-row="1" data-col="3">8</td>
                  <td data-row="1" data-col="4">9</td>
                </tr>
<tr>
<td data-row="2" data-col="0"></td>
                  <td data-row="2" data-col="1">% row</td>
                  <td data-row="2" data-col="2">11.1</td>
                  <td data-row="2" data-col="3">88.9</td>
                  <td data-row="2" data-col="4">100.0</td>
                </tr>
<tr>
<td data-row="3" data-col="0">6</td>
                  <td data-row="3" data-col="1">N</td>
                  <td data-row="3" data-col="2">2</td>
                  <td data-row="3" data-col="3">3</td>
                  <td data-row="3" data-col="4">5</td>
                </tr>
<tr>
<td data-row="4" data-col="0"></td>
                  <td data-row="4" data-col="1">% row</td>
                  <td data-row="4" data-col="2">40.0</td>
                  <td data-row="4" data-col="3">60.0</td>
                  <td data-row="4" data-col="4">100.0</td>
                </tr>
<tr>
<td data-row="5" data-col="0">8</td>
                  <td data-row="5" data-col="1">N</td>
                  <td data-row="5" data-col="2">14</td>
                  <td data-row="5" data-col="3">0</td>
                  <td data-row="5" data-col="4">14</td>
                </tr>
<tr>
<td data-row="6" data-col="0"></td>
                  <td data-row="6" data-col="1">% row</td>
                  <td data-row="6" data-col="2">100.0</td>
                  <td data-row="6" data-col="3">0.0</td>
                  <td data-row="6" data-col="4">100.0</td>
                </tr>
<tr>
<td data-row="7" data-col="0">NA</td>
                  <td data-row="7" data-col="1">N</td>
                  <td data-row="7" data-col="2">1</td>
                  <td data-row="7" data-col="3">3</td>
                  <td data-row="7" data-col="4">4</td>
                </tr>
<tr>
<td data-row="8" data-col="0"></td>
                  <td data-row="8" data-col="1">% row</td>
                  <td data-row="8" data-col="2">25.0</td>
                  <td data-row="8" data-col="3">75.0</td>
                  <td data-row="8" data-col="4">100.0</td>
                </tr>
<tr>
<td data-row="9" data-col="0">All</td>
                  <td data-row="9" data-col="1">N</td>
                  <td data-row="9" data-col="2">18</td>
                  <td data-row="9" data-col="3">14</td>
                  <td data-row="9" data-col="4">32</td>
                </tr>
<tr>
<td data-row="10" data-col="0"></td>
                  <td data-row="10" data-col="1">% row</td>
                  <td data-row="10" data-col="2">56.2</td>
                  <td data-row="10" data-col="3">43.8</td>
                  <td data-row="10" data-col="4">100.0</td>
                </tr>
</tbody>
</table>
</div>
<!-- hack to avoid NA insertion in last line -->
</div>
</div>
</section><section id="output-formats" class="level2"><h2 class="anchored" data-anchor-id="output-formats">Output formats</h2>
<p>All functions in the <code>datasummary_*</code> family accept the same <code>output</code> argument. Tables can be saved to several file formats:</p>
<div class="cell">
<div class="sourceCode" id="cb63"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va">f</span> <span class="op">&lt;-</span> <span class="va">flipper_length_mm</span> <span class="op">~</span> <span class="va">island</span> <span class="op">*</span> <span class="op">(</span><span class="va">mean</span> <span class="op">+</span> <span class="va">sd</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">f</span>, data <span class="op">=</span> <span class="va">penguins</span>, output <span class="op">=</span> <span class="st">'table.html'</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">f</span>, data <span class="op">=</span> <span class="va">penguins</span>, output <span class="op">=</span> <span class="st">'table.tex'</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">f</span>, data <span class="op">=</span> <span class="va">penguins</span>, output <span class="op">=</span> <span class="st">'table.docx'</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">f</span>, data <span class="op">=</span> <span class="va">penguins</span>, output <span class="op">=</span> <span class="st">'table.pptx'</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">f</span>, data <span class="op">=</span> <span class="va">penguins</span>, output <span class="op">=</span> <span class="st">'table.md'</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">f</span>, data <span class="op">=</span> <span class="va">penguins</span>, output <span class="op">=</span> <span class="st">'table.rtf'</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">f</span>, data <span class="op">=</span> <span class="va">penguins</span>, output <span class="op">=</span> <span class="st">'table.jpg'</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">f</span>, data <span class="op">=</span> <span class="va">penguins</span>, output <span class="op">=</span> <span class="st">'table.png'</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>They can be returned in human-readable data.frames, markdown, html, or LaTeX code to the console:</p>
<div class="cell">
<div class="sourceCode" id="cb64"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">f</span>, data <span class="op">=</span> <span class="va">penguins</span>, output <span class="op">=</span> <span class="st">'data.frame'</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">f</span>, data <span class="op">=</span> <span class="va">penguins</span>, output <span class="op">=</span> <span class="st">'markdown'</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">f</span>, data <span class="op">=</span> <span class="va">penguins</span>, output <span class="op">=</span> <span class="st">'html'</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">f</span>, data <span class="op">=</span> <span class="va">penguins</span>, output <span class="op">=</span> <span class="st">'latex'</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p><code>datasummary</code> can return objects compatible with the <code>tinytable</code>, <code>gt</code>, <code>kableExtra</code>, <code>flextable</code>, <code>huxtable</code>, and <code>DT</code> table creation and customization packages:</p>
<div class="cell">
<div class="sourceCode" id="cb65"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">f</span>, data <span class="op">=</span> <span class="va">penguins</span>, output <span class="op">=</span> <span class="st">'tinytable'</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">f</span>, data <span class="op">=</span> <span class="va">penguins</span>, output <span class="op">=</span> <span class="st">'gt'</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">f</span>, data <span class="op">=</span> <span class="va">penguins</span>, output <span class="op">=</span> <span class="st">'kableExtra'</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">f</span>, data <span class="op">=</span> <span class="va">penguins</span>, output <span class="op">=</span> <span class="st">'flextable'</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">f</span>, data <span class="op">=</span> <span class="va">penguins</span>, output <span class="op">=</span> <span class="st">'huxtable'</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://modelsummary.com/man/datasummary.html">datasummary</a></span><span class="op">(</span><span class="va">f</span>, data <span class="op">=</span> <span class="va">penguins</span>, output <span class="op">=</span> <span class="st">'DT'</span><span class="op">)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>Please note that hierarchical or “nested” column labels are only available for these output formats: kableExtra, gt, html, rtf, and LaTeX. When saving tables to other formats, nested labels will be combined to a “flat” header.</p>


</section></section></main><!-- /main --><script id="quarto-html-after-body" type="application/javascript">
  window.document.addEventListener("DOMContentLoaded", function (event) {
    const icon = "";
    const anchorJS = new window.AnchorJS();
    anchorJS.options = {
      placement: 'right',
      icon: icon
    };
    anchorJS.add('.anchored');
    const isCodeAnnotation = (el) => {
      for (const clz of el.classList) {
        if (clz.startsWith('code-annotation-')) {                     
          return true;
        }
      }
      return false;
    }
    const onCopySuccess = function(e) {
      // button target
      const button = e.trigger;
      // don't keep focus
      button.blur();
      // flash "checked"
      button.classList.add('code-copy-button-checked');
      var currentTitle = button.getAttribute("title");
      button.setAttribute("title", "Copied!");
      let tooltip;
      if (window.bootstrap) {
        button.setAttribute("data-bs-toggle", "tooltip");
        button.setAttribute("data-bs-placement", "left");
        button.setAttribute("data-bs-title", "Copied!");
        tooltip = new bootstrap.Tooltip(button, 
          { trigger: "manual", 
            customClass: "code-copy-button-tooltip",
            offset: [0, -8]});
        tooltip.show();    
      }
      setTimeout(function() {
        if (tooltip) {
          tooltip.hide();
          button.removeAttribute("data-bs-title");
          button.removeAttribute("data-bs-toggle");
          button.removeAttribute("data-bs-placement");
        }
        button.setAttribute("title", currentTitle);
        button.classList.remove('code-copy-button-checked');
      }, 1000);
      // clear code selection
      e.clearSelection();
    }
    const getTextToCopy = function(trigger) {
        const codeEl = trigger.previousElementSibling.cloneNode(true);
        for (const childEl of codeEl.children) {
          if (isCodeAnnotation(childEl)) {
            childEl.remove();
          }
        }
        return codeEl.innerText;
    }
    const clipboard = new window.ClipboardJS('.code-copy-button:not([data-in-quarto-modal])', {
      text: getTextToCopy
    });
    clipboard.on('success', onCopySuccess);
    if (window.document.getElementById('quarto-embedded-source-code-modal')) {
      const clipboardModal = new window.ClipboardJS('.code-copy-button[data-in-quarto-modal]', {
        text: getTextToCopy,
        container: window.document.getElementById('quarto-embedded-source-code-modal')
      });
      clipboardModal.on('success', onCopySuccess);
    }
      var localhostRegex = new RegExp(/^(?:http|https):\/\/localhost\:?[0-9]*\//);
      var mailtoRegex = new RegExp(/^mailto:/);
        var filterRegex = new RegExp('/' + window.location.host + '/');
      var isInternal = (href) => {
          return filterRegex.test(href) || localhostRegex.test(href) || mailtoRegex.test(href);
      }
      // Inspect non-navigation links and adorn them if external
     var links = window.document.querySelectorAll('a[href]:not(.nav-link):not(.navbar-brand):not(.toc-action):not(.sidebar-link):not(.sidebar-item-toggle):not(.pagination-link):not(.no-external):not([aria-hidden]):not(.dropdown-item):not(.quarto-navigation-tool):not(.about-link)');
      for (var i=0; i<links.length; i++) {
        const link = links[i];
        if (!isInternal(link.href)) {
          // undo the damage that might have been done by quarto-nav.js in the case of
          // links that we want to consider external
          if (link.dataset.originalHref !== undefined) {
            link.href = link.dataset.originalHref;
          }
        }
      }
    function tippyHover(el, contentFn, onTriggerFn, onUntriggerFn) {
      const config = {
        allowHTML: true,
        maxWidth: 500,
        delay: 100,
        arrow: false,
        appendTo: function(el) {
            return el.parentElement;
        },
        interactive: true,
        interactiveBorder: 10,
        theme: 'quarto',
        placement: 'bottom-start',
      };
      if (contentFn) {
        config.content = contentFn;
      }
      if (onTriggerFn) {
        config.onTrigger = onTriggerFn;
      }
      if (onUntriggerFn) {
        config.onUntrigger = onUntriggerFn;
      }
      window.tippy(el, config); 
    }
    const noterefs = window.document.querySelectorAll('a[role="doc-noteref"]');
    for (var i=0; i<noterefs.length; i++) {
      const ref = noterefs[i];
      tippyHover(ref, function() {
        // use id or data attribute instead here
        let href = ref.getAttribute('data-footnote-href') || ref.getAttribute('href');
        try { href = new URL(href).hash; } catch {}
        const id = href.replace(/^#\/?/, "");
        const note = window.document.getElementById(id);
        if (note) {
          return note.innerHTML;
        } else {
          return "";
        }
      });
    }
    const xrefs = window.document.querySelectorAll('a.quarto-xref');
    const processXRef = (id, note) => {
      // Strip column container classes
      const stripColumnClz = (el) => {
        el.classList.remove("page-full", "page-columns");
        if (el.children) {
          for (const child of el.children) {
            stripColumnClz(child);
          }
        }
      }
      stripColumnClz(note)
      if (id === null || id.startsWith('sec-')) {
        // Special case sections, only their first couple elements
        const container = document.createElement("div");
        if (note.children && note.children.length > 2) {
          container.appendChild(note.children[0].cloneNode(true));
          for (let i = 1; i < note.children.length; i++) {
            const child = note.children[i];
            if (child.tagName === "P" && child.innerText === "") {
              continue;
            } else {
              container.appendChild(child.cloneNode(true));
              break;
            }
          }
          if (window.Quarto?.typesetMath) {
            window.Quarto.typesetMath(container);
          }
          return container.innerHTML
        } else {
          if (window.Quarto?.typesetMath) {
            window.Quarto.typesetMath(note);
          }
          return note.innerHTML;
        }
      } else {
        // Remove any anchor links if they are present
        const anchorLink = note.querySelector('a.anchorjs-link');
        if (anchorLink) {
          anchorLink.remove();
        }
        if (window.Quarto?.typesetMath) {
          window.Quarto.typesetMath(note);
        }
        if (note.classList.contains("callout")) {
          return note.outerHTML;
        } else {
          return note.innerHTML;
        }
      }
    }
    for (var i=0; i<xrefs.length; i++) {
      const xref = xrefs[i];
      tippyHover(xref, undefined, function(instance) {
        instance.disable();
        let url = xref.getAttribute('href');
        let hash = undefined; 
        if (url.startsWith('#')) {
          hash = url;
        } else {
          try { hash = new URL(url).hash; } catch {}
        }
        if (hash) {
          const id = hash.replace(/^#\/?/, "");
          const note = window.document.getElementById(id);
          if (note !== null) {
            try {
              const html = processXRef(id, note.cloneNode(true));
              instance.setContent(html);
            } finally {
              instance.enable();
              instance.show();
            }
          } else {
            // See if we can fetch this
            fetch(url.split('#')[0])
            .then(res => res.text())
            .then(html => {
              const parser = new DOMParser();
              const htmlDoc = parser.parseFromString(html, "text/html");
              const note = htmlDoc.getElementById(id);
              if (note !== null) {
                const html = processXRef(id, note);
                instance.setContent(html);
              } 
            }).finally(() => {
              instance.enable();
              instance.show();
            });
          }
        } else {
          // See if we can fetch a full url (with no hash to target)
          // This is a special case and we should probably do some content thinning / targeting
          fetch(url)
          .then(res => res.text())
          .then(html => {
            const parser = new DOMParser();
            const htmlDoc = parser.parseFromString(html, "text/html");
            const note = htmlDoc.querySelector('main.content');
            if (note !== null) {
              // This should only happen for chapter cross references
              // (since there is no id in the URL)
              // remove the first header
              if (note.children.length > 0 && note.children[0].tagName === "HEADER") {
                note.children[0].remove();
              }
              const html = processXRef(null, note);
              instance.setContent(html);
            } 
          }).finally(() => {
            instance.enable();
            instance.show();
          });
        }
      }, function(instance) {
      });
    }
        let selectedAnnoteEl;
        const selectorForAnnotation = ( cell, annotation) => {
          let cellAttr = 'data-code-cell="' + cell + '"';
          let lineAttr = 'data-code-annotation="' +  annotation + '"';
          const selector = 'span[' + cellAttr + '][' + lineAttr + ']';
          return selector;
        }
        const selectCodeLines = (annoteEl) => {
          const doc = window.document;
          const targetCell = annoteEl.getAttribute("data-target-cell");
          const targetAnnotation = annoteEl.getAttribute("data-target-annotation");
          const annoteSpan = window.document.querySelector(selectorForAnnotation(targetCell, targetAnnotation));
          const lines = annoteSpan.getAttribute("data-code-lines").split(",");
          const lineIds = lines.map((line) => {
            return targetCell + "-" + line;
          })
          let top = null;
          let height = null;
          let parent = null;
          if (lineIds.length > 0) {
              //compute the position of the single el (top and bottom and make a div)
              const el = window.document.getElementById(lineIds[0]);
              top = el.offsetTop;
              height = el.offsetHeight;
              parent = el.parentElement.parentElement;
            if (lineIds.length > 1) {
              const lastEl = window.document.getElementById(lineIds[lineIds.length - 1]);
              const bottom = lastEl.offsetTop + lastEl.offsetHeight;
              height = bottom - top;
            }
            if (top !== null && height !== null && parent !== null) {
              // cook up a div (if necessary) and position it 
              let div = window.document.getElementById("code-annotation-line-highlight");
              if (div === null) {
                div = window.document.createElement("div");
                div.setAttribute("id", "code-annotation-line-highlight");
                div.style.position = 'absolute';
                parent.appendChild(div);
              }
              div.style.top = top - 2 + "px";
              div.style.height = height + 4 + "px";
              div.style.left = 0;
              let gutterDiv = window.document.getElementById("code-annotation-line-highlight-gutter");
              if (gutterDiv === null) {
                gutterDiv = window.document.createElement("div");
                gutterDiv.setAttribute("id", "code-annotation-line-highlight-gutter");
                gutterDiv.style.position = 'absolute';
                const codeCell = window.document.getElementById(targetCell);
                const gutter = codeCell.querySelector('.code-annotation-gutter');
                gutter.appendChild(gutterDiv);
              }
              gutterDiv.style.top = top - 2 + "px";
              gutterDiv.style.height = height + 4 + "px";
            }
            selectedAnnoteEl = annoteEl;
          }
        };
        const unselectCodeLines = () => {
          const elementsIds = ["code-annotation-line-highlight", "code-annotation-line-highlight-gutter"];
          elementsIds.forEach((elId) => {
            const div = window.document.getElementById(elId);
            if (div) {
              div.remove();
            }
          });
          selectedAnnoteEl = undefined;
        };
          // Handle positioning of the toggle
      window.addEventListener(
        "resize",
        throttle(() => {
          elRect = undefined;
          if (selectedAnnoteEl) {
            selectCodeLines(selectedAnnoteEl);
          }
        }, 10)
      );
      function throttle(fn, ms) {
      let throttle = false;
      let timer;
        return (...args) => {
          if(!throttle) { // first call gets through
              fn.apply(this, args);
              throttle = true;
          } else { // all the others get throttled
              if(timer) clearTimeout(timer); // cancel #2
              timer = setTimeout(() => {
                fn.apply(this, args);
                timer = throttle = false;
              }, ms);
          }
        };
      }
        // Attach click handler to the DT
        const annoteDls = window.document.querySelectorAll('dt[data-target-cell]');
        for (const annoteDlNode of annoteDls) {
          annoteDlNode.addEventListener('click', (event) => {
            const clickedEl = event.target;
            if (clickedEl !== selectedAnnoteEl) {
              unselectCodeLines();
              const activeEl = window.document.querySelector('dt[data-target-cell].code-annotation-active');
              if (activeEl) {
                activeEl.classList.remove('code-annotation-active');
              }
              selectCodeLines(clickedEl);
              clickedEl.classList.add('code-annotation-active');
            } else {
              // Unselect the line
              unselectCodeLines();
              clickedEl.classList.remove('code-annotation-active');
            }
          });
        }
    const findCites = (el) => {
      const parentEl = el.parentElement;
      if (parentEl) {
        const cites = parentEl.dataset.cites;
        if (cites) {
          return {
            el,
            cites: cites.split(' ')
          };
        } else {
          return findCites(el.parentElement)
        }
      } else {
        return undefined;
      }
    };
    var bibliorefs = window.document.querySelectorAll('a[role="doc-biblioref"]');
    for (var i=0; i<bibliorefs.length; i++) {
      const ref = bibliorefs[i];
      const citeInfo = findCites(ref);
      if (citeInfo) {
        tippyHover(citeInfo.el, function() {
          var popup = window.document.createElement('div');
          citeInfo.cites.forEach(function(cite) {
            var citeDiv = window.document.createElement('div');
            citeDiv.classList.add('hanging-indent');
            citeDiv.classList.add('csl-entry');
            var biblioDiv = window.document.getElementById('ref-' + cite);
            if (biblioDiv) {
              citeDiv.innerHTML = biblioDiv.innerHTML;
            }
            popup.appendChild(citeDiv);
          });
          return popup.innerHTML;
        });
      }
    }
  });
  </script><nav class="page-navigation"><div class="nav-page nav-page-previous">
      <a href="../vignettes/get_started.html" class="pagination-link" aria-label="Getting started">
        <i class="bi bi-arrow-left-short"></i> <span class="nav-page-text">Getting started</span>
      </a>          
  </div>
  <div class="nav-page nav-page-next">
      <a href="../vignettes/modelsummary.html" class="pagination-link" aria-label="Model Summaries">
        <span class="nav-page-text">Model Summaries</span> <i class="bi bi-arrow-right-short"></i>
      </a>
  </div>
</nav>
</div> <!-- /content -->



</body></html>